自动传输 Google sheet 生成的二维码到 Google 文档作为图像

Auto Transfer Google sheet generated QR code to Google Doc as Image

我有一个 google sheet 可以自动将数据从 google sheet 传输到 google 文档。现在我在 google sheet 中生成了一个二维码,但是当我将它作为其他数据值从 google sheet 传输到 google 文档时,它说 Cellimage in google 文档。我想要 Google 文档上实际生成的二维码。我正在使用应用程序脚本根据 Jeffery 先生在 youtube 上的视频传输数据。任何帮助将不胜感激。

使用 A 列和 B 列生成二维码

function onOpen() {

    const ui = SpreadsheetApp.getUi();
    const menu = ui.createMenu('GR Filled Form');
    menu.addItem('Create New Docs',   'createNewGoogleDocs')
    menu.addToUi();
}

    


function createNewGoogleDocs() {

      const googleDocTemplate = DriveApp.getFileById('Template ID');
 //This value should be the id of the folder where you want your completed documents stored




     const destinationFolder = DriveApp.getFolderById('FolderID')
 //Here we store the sheet as a variable




    const Sheet1 = SpreadsheetApp
    .getActiveSpreadsheet();
    var sheet = Sheet1.getActiveSheet();

    var only  = ['Gen4A','Gen4B','Gen4C','Gen4D','Gen4E','Gen5']
    if (only.indexOf(sheet.getName()) == -1)
    return;





 //Now we get all of the values as a 2D  



     const rows = sheet.getDataRange().getValues();




//Start processing each spreadsheet row




     rows.forEach(function(row, index){


     if (index === 0) return;

     if (row[9]) return;
    //Using the row data in a template literal, we make a copy of our template document in our destinationFolder



        const copy = googleDocTemplate.makeCopy(`${row[1]}, ${row[0]} GR FORM` , destinationFolder)
    //Once we have the copy, we then open it using the DocumentApp



    const doc = DocumentApp.openById(copy.getId())
    //All of the content lives in the body, so we get that for editing
    const body = doc.getBody();
    //In this line we do some friendly date formatting, that may or may not work for you locale
    const friendlyDate = new Date(row[3]).toLocaleDateString();
    //const pdfFolder = DriveApp.getFolderById("folderID");
    //In these lines, we replace our replacement tokens with values from our spreadsheet row



    body.replaceText('{{Pallet}}', row[1]);
    body.replaceText('{{Batch}}', row[0]);
    body.replaceText('{{Qty}}', row[2]);
    body.replaceText('{{Date}}', friendlyDate);
    body.replaceText('{{QR Code Image}}', row[4]);
    
    
    //We make our changes permanent by saving and closing the document
    doc.saveAndClose();
    //const blobPDF = doc.getAs(MimeType.PDF);
    //const pdfFile = pdfFolder.createFile(blobPDF).setName(row[0]+"-"+friendlyDate);
       
    //Store the url of our new document in a variable
    //const url = pdfFile.getUrl();
    const url = doc.getUrl();
    //Write that value back to the 'Document Link' column in the spreadsheet. 
    sheet.getRange(index + 1, 10).setValue(url)
    
  })
  
}

sample spreadsheet

根据您显示的电子表格图片,我确认“E”列中有二维码图片。但是,从您提供的示例电子表格中,我还确认“E”列的公式类似于 =IMAGE("https://api.qrserver.com/v1/create-qr-code/?size=160x160&data="&ENCODEURL(A2&" - "&B2),3)。在这种情况下,我认为可以通过检索URL来检索二维码。在此答案中,QR 码是从 URL 中检索并放入 Google 文档中。

使用您提供的示例脚本时,进行以下修改如何?

修改后的脚本:

在使用此脚本之前,请设置 'Template ID''FolderID'

function createNewGoogleDocs() {
  // Ref: 
  var replaceTextToImage = function (body, searchText, url, width = 200) {
    var next = body.findText(searchText);
    if (!next) return;
    var r = next.getElement();
    r.asText().setText("");
    var img = r.getParent().asParagraph().insertInlineImage(0, UrlFetchApp.fetch(url).getBlob());
    if (width && typeof width == "number") {
      var w = img.getWidth();
      var h = img.getHeight();
      img.setWidth(width);
      img.setHeight(width * h / w);
    }
    return next;
  };

  const googleDocTemplate = DriveApp.getFileById('Template ID');
  const destinationFolder = DriveApp.getFolderById('FolderID');
  const Sheet1 = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = Sheet1.getActiveSheet();
  var only = ['Gen4A', 'Gen4B', 'Gen4C', 'Gen4D', 'Gen4E', 'Gen5']
  if (only.indexOf(sheet.getName()) == -1) return;
  const rows = sheet.getDataRange().getValues();
  rows.forEach(function (row, index) {
    if (index === 0) return;
    if (row[9]) return;
    const copy = googleDocTemplate.makeCopy(`${row[1]}, ${row[0]} GR FORM`, destinationFolder)
    const doc = DocumentApp.openById(copy.getId())
    const body = doc.getBody();
    const friendlyDate = new Date(row[3]).toLocaleDateString();
    body.replaceText('{{Pallet}}', row[1]);
    body.replaceText('{{Batch}}', row[0]);
    body.replaceText('{{Qty}}', row[2]);
    body.replaceText('{{Date}}', friendlyDate);
    replaceTextToImage(body, '{{QR Code Image}}', "https://api.qrserver.com/v1/create-qr-code/?size=160x160&data=" + encodeURIComponent(row[0] - row[1]));
    doc.saveAndClose();
    const url = doc.getUrl();
    sheet.getRange(index + 1, 10).setValue(url)
  });
}

注:

  • 此修改后的脚本可用于您的示例电子表格。因此,当您更改了 Spreadsheet 的结构并且您的实际情况与您的示例 Spreadsheet 不同时,此脚本可能无法使用。所以请注意这一点。

参考文献: