使用Google Apps脚本将图片从云端硬盘插入Google Spresheet单元格

使用Google Apps脚本将图片从云端硬盘插入Google Spresheet单元格

问题描述:

我已经遍历有关此主题的所有主题.从Blob到URL等,都可以通过apps-script将图像插入到单元格中,但似乎无济于事.而且大多数帖子还没有提供足够好的解决方案来追溯错误.所以我想在这里找到我的问题的答案.在我的代码下面.我也很想讨论不同方法的优点.据我所知,在处理G-Drive中已经存在的图像时,Blob似乎是最简单的方法之一.

I've made my way through all threads about this topic. From blob to URLs and more, to insert an image into a cell via apps-script but nothing seems to work. And most of the posts haven't gotten the solution laid out well enough to traceback the errors. So I'm trying to get an answer to my problem here. Below my code. I would also love to discuss the advantages of different methods. Blob seems to be one of the easiest when working with images already existing in G-Drive as far as I see.

function getGDriveFilesIntoCell() {

  var dApp = DriveApp;  // Store GDrive App in a function
  var folderIter = dApp.getFoldersByName("Training_folder");  // Get folder by name
  var foundfolder = folderIter.next();  // next allows to Iterate through the found folders
  Logger.log(foundfolder);  // Print into the logs the found folder of foundfolder

  var ss = SpreadsheetApp.getActiveSpreadsheet(); //.getActiveSheet();  // Calls the Spreadsheet App and the current active Spreadsheet
  var sheet = ss.getSheets()[0];  // Get first sheet

  var filesIter = foundfolder.getFiles();  // Get files in found folder

  var i = 1;  // Define a var with value 1

  while(filesIter.hasNext()){  // While loop for all files in folder found by name
    var file = filesIter.next();
    var filename = file.getName();  // Get Name
    var filesize = file.getSize() / 1024;  // Get size in kb
    var file_id = file.getId();  // Get ID
    var file_url = file.getUrl();  // Get Url to file
    sheet.insertImage(file.getBlob(), i, 1);  // Insert Image
    Logger.log(filename + filesize + file_id);
    Logger.log(filesize);
    Logger.log(file_id);
    i++;  // increment i by one

    // file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW)  // Set file permission

    // var file_blob = file.getBlob();  // Get blob

    // Insert Image via custom bblob
    // var file_mime_type = file.getMimeType();  // Get Mime Type
    // var response = UrlFetchApp.fetch(file_url);
    // var binaryData = response.getContent();
    // var blob = Utilities.newBlob(binaryData, file_mime_type, filename);
    // sheet.insertImage(blob, i, 1);

    // Inser image via Link
    // ss.getRange(i, 1).setValue("=image(\"https://drive.google.com/uc?export=view&id=" + file.getId() +"\")");  // Adds an Image via Link
    // var img = Drive.Files.get(file.getId()).webContentLink;
  }

}

我相信您的目标如下.

  • 您要使用带有斑点的insertImage将图像放入Google Spreadsheet.
  • 您要从特定文件夹中检索图像.
  • You want to put the images to Google Spreadsheet using insertImage with the blob.
  • You want to retrieve the images from the specific folder.

为此,这个答案如何?

  • 将图像放入电子表格时,请注意以下几点.
  • When the image is put to the Spreadsheet, please be careful the following points.
  1. 在脚本中,当Training_folder文件夹中包含除图像以外的文件时,就会发生错误.
  2. 当图像尺寸超过最大限制时,会发生错误. 参考
  1. In your script, when the files except for the image are included in the folder of Training_folder, an error occurs.
  2. When the size of images are over the maximum limitation, an error occurs. Ref

我认为这些可能是您问题的原因.所以我想提出以下修改.

I thought that these might be the reason of your issue. So I would like to propose the following modification.

修改脚本后,请进行以下修改. 在此修改中,使用了Google Apps脚本库"ImgApp".因此,在运行脚本之前,请先安装GAS库.您可以在此处. >

发件人:

When your script is modified, please modify as follows. In this modification, "ImgApp" which is Google Apps Script library is used. So before you run the script, please install the GAS library. You can see how to install it at here.

while(filesIter.hasNext()){  // While loop for all files in folder found by name
  var file = filesIter.next();
  var filename = file.getName();  // Get Name
  var filesize = file.getSize() / 1024;  // Get size in kb
  var file_id = file.getId();  // Get ID
  var file_url = file.getUrl();  // Get Url to file
  sheet.insertImage(file.getBlob(), i, 1);  // Insert Image
  Logger.log(filename + filesize + file_id);
  Logger.log(filesize);
  Logger.log(file_id);
  i++;  // increment i by one
}

收件人:

while(filesIter.hasNext()){
  var file = filesIter.next();
  if (file.getMimeType().indexOf("image") != -1) {
    var blob = file.getBlob();
    var size = ImgApp.getSize(blob);
    if (size.width * size.height > 1048576) {
      var resized = ImgApp.doResize(file.getId(), 512);
      blob = resized.blob;
    }
    sheet.insertImage(blob, i, 1);  // Insert Image
    i++;  // increment i by one
  }
}

  • 在此修改后的脚本中,检索图像文件.并且,当图像大小超过电子表格的最大大小时,将调整图像大小.
    • 您可以从脚本中使用不带V8的脚本.所以我做了修改.