Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/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 如何将公式生成的二维码作为图像放入谷歌文档(导出为PDF)?_Google Apps Script_Pdf Generation_Qr Code_Google Sheets Api - Fatal编程技术网

Google apps script 如何将公式生成的二维码作为图像放入谷歌文档(导出为PDF)?

Google apps script 如何将公式生成的二维码作为图像放入谷歌文档(导出为PDF)?,google-apps-script,pdf-generation,qr-code,google-sheets-api,Google Apps Script,Pdf Generation,Qr Code,Google Sheets Api,我使用图像函数(=图像(“https://chart.googleapis.com/....)在导出到PDF后,我想把它放在google文档中,但它什么也不显示,我如何把电子表格中的QRcode作为图像放在文档中。谢谢大家的帮助 下面是代码(只是示例) 只需将B列作为图像并保存为PDF。 和谷歌表在这里!! 我相信你的目标如下 您希望使用Google Apps脚本将值从Google电子表格复制到Google文档 谷歌电子表格如下 修改点: 当我看到您的示例电子表格和脚本时,您似乎将参数发送

我使用图像函数(=图像(“https://chart.googleapis.com/....)在导出到PDF后,我想把它放在google文档中,但它什么也不显示,我如何把电子表格中的QRcode作为图像放在文档中。谢谢大家的帮助

下面是代码(只是示例)

只需将B列作为图像并保存为PDF。 和谷歌表在这里!!
我相信你的目标如下

  • 您希望使用Google Apps脚本将值从Google电子表格复制到Google文档
  • 谷歌电子表格如下
修改点:
  • 当我看到您的示例电子表格和脚本时,您似乎将参数发送到函数
    createPDF
    中,比如
    createPDF(数据[1],数据[3],数据[4],数据[5],数据[6],数据[3]+“”+data[4],docFile,tempFolder,pdfFolder)
    • 电子表格的值从“A”列到“F”列。但是在您的参数中,使用了
      data[6]
      。在这种情况下,在
      body.replaceText(“{qty}”,quantity)
      处会发生错误
    • createPDF(qcode,First,Last,address,quantity,pdfName,docFile,tempFolder,pdfFolder)
      ,我认为需要
      createPDF(数据[1],数据[2],数据[3],数据[4],数据[5],数据[2]+“”+data[3],docFile,tempFolder,pdfFolder)
    • 如果您的实际电子表格与示例电子表格不同,则以下修改的脚本不起作用。因此,请注意这一点。在此回答中,我使用示例电子表格修改了您的脚本。
  • 为了从
    =image(“https://chart.googleapis.com/....
    ,我想建议使用UrlFetchApp检索图像blob。关于将文本替换为图像的脚本,我在中使用了示例脚本。
    • URL是从公式和您检索的
      数据中检索的
当上述各点反映到脚本中时,它将变成如下所示

修改脚本: 在使用此脚本之前,请设置
xxx
的每个值

function createBuikPDFs(e) {
  const pdfFolder = DriveApp.getFolderById("xxx");
  const tempFolder = DriveApp.getFolderById("xxx");
  const docFile = DriveApp.getFileById("xxx");
  const ws = SpreadsheetApp.openById("xxx").getSheetByName("eee");

  const values = ws.getRange(2, 1, ws.getLastRow() - 1, 6).getValues();
  const length = values.length;
  var data = values[length - 1];
  const formula = ws.getRange(length + 1, 2).getFormula();
  const url = formula.replace(/\=image\("/i, "").replace(/chl\="&.+/i, `chl=${data[5]}`);
  data[1] = UrlFetchApp.fetch(url).getBlob();
  createPDF(data[1], data[2], data[3], data[4], data[5], data[2] + "" + data[3], docFile, tempFolder, pdfFolder);
}

function createPDF(qcode, First, Last, address, quantity, pdfName, docFile, tempFolder, pdfFolder) {

  // This function is from https://stackoverflow.com/a/51913863
  var replaceTextToImage = function (body, searchText, image, width) {
    var next = body.findText(searchText);
    if (!next) return;
    var r = next.getElement();
    r.asText().setText("");
    var img = r.getParent().asParagraph().insertInlineImage(0, image);
    if (width && typeof width == "number") {
      var w = img.getWidth();
      var h = img.getHeight();
      img.setWidth(width);
      img.setHeight(width * h / w);
    }
    return next;
  };

  const tempFile = docFile.makeCopy(tempFolder);
  const tempDocFile = DocumentApp.openById(tempFile.getId());
  const body = tempDocFile.getBody();

  replaceTextToImage(body, "{qr}", qcode);
  body.replaceText("{fn}", First);
  body.replaceText("{ln}", Last);
  body.replaceText("{addr}", address);
  body.replaceText("{qty}", quantity);
  tempDocFile.saveAndClose();

  const pdfContentBolb = tempFile.getAs("application/pdf");
  const pdfFile = pdfFolder.createFile(pdfContentBolb).setName(pdfName);
  tempFile.setTrashed(true);

  return pdfFile;
}
注:
  • 在此修改的脚本中,使用了您的示例电子表格。请小心。
参考资料:
  • 相关线程
function createBuikPDFs(e) {
  const pdfFolder = DriveApp.getFolderById("xxx");
  const tempFolder = DriveApp.getFolderById("xxx");
  const docFile = DriveApp.getFileById("xxx");
  const ws = SpreadsheetApp.openById("xxx").getSheetByName("eee");

  const values = ws.getRange(2, 1, ws.getLastRow() - 1, 6).getValues();
  const length = values.length;
  var data = values[length - 1];
  const formula = ws.getRange(length + 1, 2).getFormula();
  const url = formula.replace(/\=image\("/i, "").replace(/chl\="&.+/i, `chl=${data[5]}`);
  data[1] = UrlFetchApp.fetch(url).getBlob();
  createPDF(data[1], data[2], data[3], data[4], data[5], data[2] + "" + data[3], docFile, tempFolder, pdfFolder);
}

function createPDF(qcode, First, Last, address, quantity, pdfName, docFile, tempFolder, pdfFolder) {

  // This function is from https://stackoverflow.com/a/51913863
  var replaceTextToImage = function (body, searchText, image, width) {
    var next = body.findText(searchText);
    if (!next) return;
    var r = next.getElement();
    r.asText().setText("");
    var img = r.getParent().asParagraph().insertInlineImage(0, image);
    if (width && typeof width == "number") {
      var w = img.getWidth();
      var h = img.getHeight();
      img.setWidth(width);
      img.setHeight(width * h / w);
    }
    return next;
  };

  const tempFile = docFile.makeCopy(tempFolder);
  const tempDocFile = DocumentApp.openById(tempFile.getId());
  const body = tempDocFile.getBody();

  replaceTextToImage(body, "{qr}", qcode);
  body.replaceText("{fn}", First);
  body.replaceText("{ln}", Last);
  body.replaceText("{addr}", address);
  body.replaceText("{qty}", quantity);
  tempDocFile.saveAndClose();

  const pdfContentBolb = tempFile.getAs("application/pdf");
  const pdfFile = pdfFolder.createFile(pdfContentBolb).setName(pdfName);
  tempFile.setTrashed(true);

  return pdfFile;
}