Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 将图像(从URL)从Google工作表单元格插入Google文档_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 将图像(从URL)从Google工作表单元格插入Google文档

Google apps script 将图像(从URL)从Google工作表单元格插入Google文档,google-apps-script,google-sheets,Google Apps Script,Google Sheets,此代码用于插入图像 但是我想从谷歌表单中获取url。我将“UrlFetchApp.fetch(“sourceSheet.getActiveCell().getValue()”)放入 在下面的代码中显示我对如何解决此问题的思考 function insertImage() { // Retrieve an image from the web. var resp = UrlFetchApp.fetch("sourceSheet.getActiveCell().getValue()

此代码用于插入图像

但是我想从谷歌表单中获取url。我将“UrlFetchApp.fetch(“sourceSheet.getActiveCell().getValue()”)放入

在下面的代码中显示我对如何解决此问题的思考

  function insertImage() {



  // Retrieve an image from the web.
  var resp = UrlFetchApp.fetch("sourceSheet.getActiveCell().getValue()");

  // Create a document.
  var doc = DocumentApp.openById("1qWnGAR_WBpHkSdY5frld0VNfHtQ6BSzGxlzVNDi5xMk");

  // Append the image to the first paragraph.
  doc.getChild(2).asParagraph().appendInlineImage(resp);
}

最终的目标是,我有一个在一列中包含文档ID的工作表,在另一列中插入图像的ulr。我希望运行脚本,以便将每个图像插入到每个文档中。

这里有一个可能的解决方案。当然,您必须用自己的文档ID替换文档和电子表格ID

function insertImage() {
  // Get the target document.
  var doc = DocumentApp.openById("1DeAGfM1PXXXXXXXXXXXXXXXXXXwjjeUhhZTfpo");

  // Get the Spreadsheet where the url is defined
  var sheet = SpreadsheetApp.openById("0Agl8XXXXXXXXXXXXXXXXXXXXXXXXXRScWU2TlE");

  // Get the url from the correct celll
  var url = sheet.getRange("A1").getValue();

  // Retrieve an image from the web.
  var resp = UrlFetchApp.fetch(url);

  // Append the image to the first paragraph.
  doc.getChild(0).asParagraph().appendInlineImage(resp.getBlob());
}
这建立在(Thx Eduardo)的基础上,并添加了迭代浏览电子表格并在每一行上执行此过程的功能。为了做到这一点,我制作了它,这样你就可以在每一行的不同文档中插入不同的图像

由于AutoCrat还不支持插入这样的图像,这是我找到的最好的解决方法

为了测试这个脚本,我将
lastRow=3
。将3更改为
sheet.getLastRow()
以完成整个工作表

还请注意,“getsheetbyname”使用单引号“XXX”,因为它有一个空格,否则会破坏这一点

function insertImage() {
    var startRow = 2;  // First row of data to process
    var lastRow = 3;   // Last row of data to process

    for (var i = startRow; i <= lastRow; i++)
    {
        // Get the Spreadsheet where the url is defined
        var sheet = SpreadsheetApp.openById("0AsrSazSXVGZtdEtQOTFpTTFzWFBhaGpDT3FWcVlIasd").getSheetByName('88. Research');


        // Get the target document.
        var docid = sheet.getRange("F"+i).getValue();

        var doc = DocumentApp.openById(docid);

        // Get the url from the correct cell
        var url = sheet.getRange("D"+i).getValue();

        // Retrieve an image from the web.
        var resp = UrlFetchApp.fetch(url);

        // Append the image to the first paragraph.
       doc.getChild(0).asParagraph().appendInlineImage(resp.getBlob());
    }

}

// replace 3 with sheet.getLastRow()
函数插入图像(){
var startRow=2;//要处理的第一行数据
var lastRow=3;//要处理的最后一行数据
对于(var i=startRow;i