从谷歌表单创建PDF

从谷歌表单创建PDF,pdf,google-apps-script,google-sheets,Pdf,Google Apps Script,Google Sheets,函数createBulkPDFs(){ const docFile=DriveApp.getFileById(“id”) const tempFolder=DriveApp.getFolderById(“id”) const pdfFolder=DriveApp.getFolderById(“id”) const currentSheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Sheet1”) const data=curre

函数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(2,1,currentSheet.getLastRow()-1,15).getDisplayValues()

让错误=[]

data.forEach(行=>{

});//关闭forEach

currentSheet.getRange(2,15,currentSheet.getLastRow()-1,1).setValues(错误)

}

作用 createpdf(名字、姓氏、描述、地址、位置、信件日期、定义日期、pdfname、文档文件、临时文件夹、PDF文件夹){

const tempFile=docFile.makeCopy(tempFolder)

const tempDocFile=DocumentApp.openById(tempFile.getId())

const body=tempDocFile.getBody()

替换文本(“{First name}”,First_name)

replaceText(“{Last name}”,Last_name)

replaceText(“{Description}”,Description)

正文.replaceText(“{Address}”,地址)

replaceText(“{Location}”,Location)

正文.替换文本(“{信函日期}”,信函日期)

正文.replaceText(“{Date of Def}”,Date of Def)

tempDocFile.saveAndClose()

const pdfContentBlob=tempFile.getAs(MineType.Pdf)


pdfFolder.createFile(pdfContentBlob).setName(“pdfname”);

我试图复制您的代码,但发现了一些问题

  • 填充数据行时使用
    setValues()
    。数据应为二维。每个子数组表示数据行
  • getAs()中使用的值应该是
    application/pdf
    ,而不是
    MineType.pdf
这是我的示例数据:

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);
}

代码:

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:

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);
}

工作表:

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);
}

参考文献:

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);
}

请附上代码、示例文档和工作表。let errors=[];data.forEach(行=>{createpdf(行[0],行[1],行[5],行[6],行[7],行[8],行[9],行[0]+“”+行[1],docFile,tempFolder,pdfFolder);errors.push(“”;}catch(error){errors.push(“失败”);};//关闭forEach currentSheet.getRange(2,15,currentSheet.getLastRow()-1,1).设置值(错误);而不是使用这些实际上不起作用的代码,但我希望对超过20列的多列使用循环,并对每行使用循环。请编辑您的帖子,并将代码与工作表中的示例数据一起附加。请参阅这些代码,我正试图运行,但无法基于excel数据创建多个pdf。可以进行哪些更改使用循环对这些代码或任何内容进行修改,或者我想向其中添加更多字段。我仍然收到错误消息,并且显示的最后一列失败。@VidhiSakhidas-错误消息是什么?@Nikko J。代码执行正确,但在最后一列中显示失败,因此无法生成pdf,也无法替换临时文件值。是否已执行您已尝试删除“重试捕获”以定位错误源?并且临时文件值未被替换。文档中的文件值未被替换。是否需要设置这些值为行标题或类似的内容