Google Apps脚本 - 在驱动器中使用文件的URL = IMAGE()电子表格公式

时间:2018-03-22 17:16:19

标签: google-apps-script google-sheets google-drive-api

在下面的代码中,我在我的云端硬盘中获取了一个文件的网址,并在公式= IMAGE()中使用了该文件。但是,图像不会显示在单元格中。我将正在检索的URL复制并粘贴到我的浏览器中,然后拉出图像文件。我还尝试输入一个不同的URL(来自Google图像搜索),并在单元格中显示图像。以下是我的代码片段,它不起作用:

//Function to populate Packing Instructions sheet
function createPackingInstructions() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var entryFormSheet = ss.getSheetByName('Entry Form');
  var packingInstructionsSheet = ss.getSheetByName('Packing Instructions');
  var poNumber = entryFormSheet.getRange(2, 2).getValue();
  var drive = DriveApp;
  var proofHorizontal = drive.getFilesByName('PO ' + poNumber + ' Proof Horizontal.png');
  var proofRange = packingInstructionsSheet.getRange(1, 7);

  Logger.log(poNumber);

  //Starts by clearing the Instructions sheet
  packingInstructionsSheet.getRange(11, 1, 30, 11).clear();
  proofRange.clearContent();

  Logger.log(proofHorizontal.hasNext());

  //Gets image file URL
  while (proofHorizontal.hasNext()) {
    var file = proofHorizontal.next();
    var proofName = file.getName();
    var proofUrl = file.getUrl();

    Logger.log(proofName);
    Logger.log(proofUrl);

    proofRange.setFormula('IMAGE("' + proofUrl + '", 1)');
  }
}

我根据here中的建议调整了代码,以使用URL的永久链接版本,但它具有相同的行为;它正确输入公式,当输入我的浏览器时URL工作,但图像不会显示在单元格中。这是更新的代码:

//Function to populate Packing Instructions sheet
function createPackingInstructions() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var entryFormSheet = ss.getSheetByName('Entry Form');
  var packingInstructionsSheet = ss.getSheetByName('Packing Instructions');
  var poNumber = entryFormSheet.getRange(2, 2).getValue();
  var drive = DriveApp;
  var proofHorizontal = drive.getFilesByName('PO ' + poNumber + ' Proof Horizontal.png');
  var proofRange = packingInstructionsSheet.getRange(1, 7);
  var baseUrl = "http://drive.google.com/uc?export=view&id=";

  Logger.log(poNumber);

  //Starts by clearing the Instructions sheet
  packingInstructionsSheet.getRange(11, 1, 30, 11).clear();
  proofRange.clearContent();

  Logger.log(proofHorizontal.hasNext());

  //Gets image file URL
  while (proofHorizontal.hasNext()) {
    var file = proofHorizontal.next();
    //file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
    var proofId = file.getId();
    var proofUrl = baseUrl+proofId;

    Logger.log(proofUrl);

    proofRange.setFormula('IMAGE("' + proofUrl + '", 1)');
  }
}

1 个答案:

答案 0 :(得分:1)

自我提出问题,因为我找到了一个有效的解决方案:

我上面的第二个代码示例很接近,但您需要使用"下载" URL的版本,而不是"视图"版。见下文:

var baseUrl = "https://drive.google.com/uc?export=download&id=";

//Gets image file URL
while (proofHorizontal.hasNext()) {
  var file = proofHorizontal.next();
  //This line may be necessary, depending on permissions
  //file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
  var proofId = file.getId();
  var proofUrl = baseUrl+proofId;
}

以下是有关此信息的链接:https://blog.appsevents.com/2014/04/how-to-bypass-google-drive-viewer-and.html