简体   繁体   English

从谷歌表格创建 PDF

[英]CREATE PDF FROM GOOGLE SHEETS

function createBulkPDFs(){ function createBulkPDFs(){

const docFile = DriveApp.getFileById("id");常量 docFile = DriveApp.getFileById("id");

const tempFolder = DriveApp.getFolderById("id"); const tempFolder = DriveApp.getFolderById("id");

const pdfFolder = DriveApp.getFolderById("id");常量 pdfFolder = DriveApp.getFolderById("id");

const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1") const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")

const data = currentSheet.getRange(2,1,currentSheet.getLastRow()-1,15).getDisplayValues();常量数据 = currentSheet.getRange(2,1,currentSheet.getLastRow()-1,15).getDisplayValues();

let errors = [];让错误 = [];

data.forEach(row => { data.forEach(行 => {

try{

 createpdf(row[0],row[1],row[5],row[6],row[7],row[8],row[9],row[0] + " " + row[1],docFile,tempFolder,pdfFolder);
 errors.push("");
} catch(err){
 errors.push("Failed");
}

}); }); //close forEach //关闭forEach

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

} }

function createpdf(First_name,Last_name,Description,Address,Location,Date_of_letter,Date_of_Def,pdfname,docFile,tempFolder,pdfFolder) { function createpdf(名字,姓氏,描述,地址,位置,Date_of_letter,Date_of_Def,pdfname,docFile,tempFolder,pdfFolder){

const tempFile = docFile.makeCopy(tempFolder);常量 tempFile = docFile.makeCopy(tempFolder);

const tempDocFile = DocumentApp.openById(tempFile.getId());常量 tempDocFile = DocumentApp.openById(tempFile.getId());

const body = tempDocFile.getBody(); const body = tempDocFile.getBody();

body.replaceText("{First name}",First_name); body.replaceText("{名字}",First_name);

body.replaceText("{Last name}",Last_name); body.replaceText("{姓}",Last_name);

body.replaceText("{Description}",Description); body.replaceText("{Description}",Description);

body.replaceText("{Address}",Address); body.replaceText("{地址}",地址);

body.replaceText("{Location}",Location); body.replaceText("{Location}",Location);

body.replaceText("{Date of letter}",Date_of_letter); body.replaceText("{字母日期}",Date_of_letter);

body.replaceText("{Date of Def}",Date_of_Def); body.replaceText("{Date of Def}",Date_of_Def);

tempDocFile.saveAndClose(); tempDocFile.saveAndClose();

const pdfContentBlob = tempFile.getAs(MineType.Pdf);常量 pdfContentBlob = tempFile.getAs(MineType.Pdf);

pdfFolder.createFile(pdfContentBlob).setName("pdfname"); pdfFolder.createFile(pdfContentBlob).setName("pdfname");

I tried to replicate your code and found some issues.我试图复制您的代码并发现了一些问题。

  • The use of setValues() when populating rows of data.填充数据行时使用setValues() The data should be in 2 dimensional.数据应该是二维的。 Each sub-array represents row of data.每个子数组代表一行数据。
  • The value used in getAs() should be application/pdf instead of MineType.pdf . getAs() 中使用的值应该是application/pdf而不是MineType.pdf

Here is my sample data:这是我的示例数据:

样本数据

Code:代码:

function createBulkPDFs() {
    const docFile = DriveApp.getFileById("id");
    const tempFolder = DriveApp.getFolderById("id");
    const pdfFolder = DriveApp.getFolderById("id");
    const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")
    const data = currentSheet.getRange(1, 1, currentSheet.getLastRow(), 11).getDisplayValues();
    let errors = [];
    data.forEach(row => {
        try {
            createpdf(row[0], row[1], row[5], row[6], row[7], row[8], row[9], row[0] + " " + row[1], docFile, tempFolder, pdfFolder);
            errors.push("Success");
        } catch (err) {
            errors.push("Failed");
        }
    }); //close forEach
    let newArr = [];
    while(errors.length > 0) {
        newArr.push(errors.splice(0,1));
    }
    currentSheet.getRange(1, 12, currentSheet.getLastRow(), 1).setValues(newArr);
}


function createpdf(First_name, Last_name, Description, Address, Location, Date_of_letter, Date_of_Def, pdfname, docFile, tempFolder, pdfFolder){
    const tempFile = docFile.makeCopy(tempFolder);
    const tempDocFile = DocumentApp.openById(tempFile.getId());
    const body = tempDocFile.getBody();
    body.replaceText("{First name}", First_name);
    body.replaceText("{Last name}", Last_name);
    body.replaceText("{Description}", Description);
    body.replaceText("{Address}", Address);
    body.replaceText("{Location}", Location);
    body.replaceText("{Date of letter}", Date_of_letter);
    body.replaceText("{Date of Def}", Date_of_Def);
    tempDocFile.saveAndClose();
    const pdfContentBlob = tempFile.getAs('application/pdf');
    pdfFolder.createFile(pdfContentBlob).setName(pdfname);
}

PDF: PDF:

PDFS

Sheets:床单:

床单

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM