从电子表格创建 PDF 需要将 PDF URL 返回到电子表格

Creating PDFs from Spreadsheet Need PDF URL to be returned to Spreadsheet

我有一个包含数据的电子表格,一个用作我的模板的文档,以及一个脚本的开头,该脚本使用电子表格和文档为每一行数据创建一个 PDF。这很好用!

我的下一步,也是我正在努力的地方,我是否需要将每个创建的 PDF(每个 PDF 转到同一个文件夹)的 URL/identifier(来自 google 驱动器)放入我的电子表格,这样我就可以创建另一个脚本来通过电子邮件向收件人发送他们的文档。

我在使用表单和触发器时看到过这样做,但由于我没有主动收集数据,所以我无法理解下一部分。

谢谢, 内特

/*This function takes the data from the Charitable Tax Receipt summary and passes one row at a time
to the createPDF function.
*/

    function createTaxRcptPDFs(){

    const DOCTEMPLATE = DriveApp.getFileById("ref");
    const TEMPFOLDER = DriveApp.getFolderById("ref1");
    const PDFFOLDER = DriveApp.getFolderById("ref2");
    const CURRENTSHEET = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TEST");
    const DATA = CURRENTSHEET.getRange(5,1,3,20).getDisplayValues(); //change 3 to formula when testing done

// this code block creates a data array to populate each PDF per (row)

    DATA.forEach(row => {

  const ACTIVECELL = row[20];
  createPDF(row[2],row[4],row[5],row[6],row[7],row[1],row[14],row[15],row[16],new Date(),row[1] + "_" + row[2],DOCTEMPLATE,TEMPFOLDER,PDFFOLDER);
  });

}


/* This function creates the PDF based on a Doc template and saves it to a specific folder
for future use.
*/

    function createPDF(fullName,street,city,state,postalCode,receiptNumber,donation,advantage,eligible,rcptDate,pdfName,DOCTEMPLATE,TEMPFOLDER,PDFFOLDER){

// constants/variables to use

    const TEMPFILE = DOCTEMPLATE.makeCopy(TEMPFOLDER);
    const TEMPDOCFILE = DocumentApp.openById(TEMPFILE.getId());

// array of values to create body from data

    const BODY = TEMPDOCFILE.getBody();
    BODY.replaceText("{fullName}", fullName);
    BODY.replaceText("{street}", street);
    BODY.replaceText("{city}", city);
    BODY.replaceText("{state}", state);
    BODY.replaceText("{postalCode}", postalCode);
    BODY.replaceText("{receiptNumber}", receiptNumber);
    BODY.replaceText("{donation}", donation);
    BODY.replaceText("{advantage}", advantage);
    BODY.replaceText("{eligible}", eligible);
    BODY.replaceText("{rcptDate}", rcptDate);
    TEMPDOCFILE.saveAndClose();

// create pdf and delete temp file

    const PDFBLOB = TEMPFILE.getAs(MimeType.PDF);
    PDFFOLDER.createFile(PDFBLOB).setName(pdfName);
    TEMPFOLDER.removeFile(TEMPFILE);

}

解决方案:

setName(pdfName) returns 用于链接的文件对象,因此您可以在之后立即使用 getDownloadUrl()

var fileUrl = PDFFOLDER.createFile(PDFBLOB).setName(pdfName).getDownloadUrl();

参考:

getDownloadUrl()