Reputation: 1
This function works for one sheet in my file.
function createBulkPDFs() {
const docFile = DriveApp.getFileById("1jcRQ3MWZFLZFJ3sgpXEC-CQ6kdhScREuxzK3YnTGidw");
const tempFolder = DriveApp.getFolderById("1GOKpDc32CrLIAqGnZimZu9LYynu8rhrn");
const pdfFolder = DriveApp.getFolderById("1iau3rHSDqiPLlf1g9VZP1ckEAt0tmuH2")
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PDF - translation");
const data = currentSheet.getRange(2,1,currentSheet.getLastRow()-1,21) .getValues();
data.forEach(row => {
createPDF(row[0],row[12],row[13],row[14],row[15],row[16],row[17],row[18],row[19],row[2],row[3],row[4],row[20],docFile,tempFolder,pdfFolder)
})
}
function createPDF(jobnumber,startDate,startMonth,startYear,startHour,startMinute,endDate,endMonth,endYear,location,customer,reference,pdfName,docFile,tempFolder,pdfFolder) {
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{jobnumber}", jobnumber);
body.replaceText("{startdate}", startDate);
body.replaceText("{startmonth}", startMonth);
body.replaceText("{startyear}", startYear);
body.replaceText("{starthour}", startHour);
body.replaceText("{startminute}", startMinute);
body.replaceText("{enddate}", endDate);
body.replaceText("{endmonth}", endMonth);
body.replaceText("{endyear}", endYear);
body.replaceText("{location}", location);
body.replaceText("{customer}", customer);
body.replaceText("{reference}", reference);
tempDocFile.saveAndClose();
const pdfContentBlob = tempFile.getAs(MimeType.PDF);
pdfFolder.createFile(pdfContentBlob).setName(pdfName);
tempFolder.removeFile(tempFile);
DriveApp.getFileById(tempFile.getId()).setTrashed(true);
}
I want to run another script for a different sheet in the same file. It's the same, just change in function name and fields. I created a new script file. And the second script always returns "TypeError: docFile.makeCopy is not a function". I tried creating new folders for the doc file, temp folder, PDF folder; and using the same folders for both scripts - none worked. This is the second script:
Can anyone help? Thanks a lot!
Upvotes: 0
Views: 65
Reputation: 8069
A script, based on two functions, one calling the other, works as expected.
This script (Script#1) has been duplicated and applied for a similar purpose but the duplicate script (Script#2) fails despite its similarity to Script#1.
The reason for the failure is that, in Script#2, the number of arguments supplied by the first function (createBulkPFDsWord
) to the second function (createPDFWord
) is not the same as the arguments expected by createPDFWord
.
createPDFWord
requires 12 arguments (refer line 11)
function createPDFWord(client,phone,date,start,end,staffname,mrn,ref,pdfName,docFile,tempFolder,pdfFolder)
But the calling function supplies only 11 arguments (refer line 8).
createPDFWord(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],docFile,tempFolder,pdfFolder)
In this case, the number of arguments supplied MUST equal the number of arguments expected.
The solution is to identify which "row" value is missing and to add this to the code in line8.
Upvotes: 1