首页 > 解决方案 > 如何将 Google 文档中的文本替换为来自 Google 表格的单元格中的图像。替换文本返回“cellimage”

问题描述

我目前正在使用一个脚本,该脚本使用谷歌表格中单元格的文本来填充谷歌文档中的特定字段,然后将其保存为 pdf。

我想用工作表中单元格中的图像(自动生成的二维码)替换谷歌文档中的一个字段,但是当我使用函数 body.replaceText 时,输出 PDF 中的替换字段包含字符串'cellimage '而不是图像。

下面是使用的脚本:

function createBulkPDFs(){
const docFile = DriveApp.getFileById("1y6hduq3CzpM5Nr8WuhlvQNZmcKViHXNWG1zvR9KgCTk");
const tempFolder = DriveApp.getFolderById("1SLj2sldcNixIe_q8RIm_LIAON1L9rImH");
const pdfFolder = DriveApp.getFolderById("1TrhUjonuXodeZWMQMJV7ZjtN4jp8WSRH");
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Customs");

const data = currentSheet.getRange(2,1,currentSheet.getLastRow()-1,20).getValues();

let errors = [];
data.forEach(row => {
  try{ 
  createPDF(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12],row[13],row[14],row[15],row[16],row[17],row[18],row[19],row [0],docFile,tempFolder,pdfFolder); //row numbers are to name columns //then this //row 0 is the NAMEof the PDF
   errors.push([""]);
  } catch(err) {
 errors.push(["Failed"]);
  }
}); //close forEach

currentSheet.getRange(2,21,currentSheet.getLastRow()-1,1).setValues(errors);

}

function createPDF(number,model,fors,length,width,thickness,litres,notes,blank,orders,dates,finSetup,lamination,artwork,finSystem,leash,finish,phoneNumber,shipping,qrcode,pdfName,docFile,tempFolder,pdfFolder) {


const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{number}",number);   //name in doc on left, named column in here on right //need to do this next!!!!
body.replaceText("{model}",model);
body.replaceText("{for}",fors);
body.replaceText("{length}",length);
body.replaceText("{width}",width);
body.replaceText("{thickness}",thickness);
body.replaceText("{litres}",litres);
body.replaceText("{notes}",notes);
body.replaceText("{blank}",blank);
body.replaceText("{orderTakenBy}",orders);
body.replaceText("{dateTaken}",dates);
body.replaceText("{finSetup}",finSetup);
body.replaceText("{lamination}",lamination);
body.replaceText("{artwork}",artwork);
body.replaceText("{finSystem}",finSystem);
body.replaceText("{leash}",leash);
body.replaceText("{finish}",finish);
body.replaceText("{phoneNumber}",phoneNumber);
body.replaceText("{shippingCo}",shipping);
body.replaceText("{qrcode}",qrcode);
tempDocFile.saveAndClose();
const pdfContentBlob = tempFile.getAs(MimeType.PDF);
pdfFolder.createFile(pdfContentBlob).setName(pdfName);
tempFolder.removeFile(tempFile)

理想情况下,二维码文本将被替换为图像(大到足以扫描 - 可能是 150 x 150)

有人可以帮我完成下一步。我是谷歌脚本的 n00b。

标签: pdfgoogle-apps-scriptgoogle-sheetspdf-generationgoogle-docs

解决方案


推荐阅读