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:
enter image description here
Can anyone help? Thanks a lot!
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:
enter image description here
Can anyone help? Thanks a lot!
Share Improve this question asked Feb 17 at 3:00 Tramy LeTramy Le 11 silver badge New contributor Tramy Le is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 7- 2 Please, do NOT post code as an image. Include the actual code in the body of the question. – Tedinoz Commented Feb 17 at 3:02
- 2 Having said that... Line#8 contains 11 arguments/variables but line#11 contains 12 arguments/variables. That is, the call to the function doesn't supply enough variables or vice versa. – Tedinoz Commented Feb 17 at 3:06
- Please provide more context on what your code is supposed to do – 4thAnd1 Commented Feb 17 at 5:49
- How are you running these functions? – 4thAnd1 Commented Feb 17 at 6:18
- 1 @Tedinoz I believe it's an answer. – TheMaster Commented Feb 17 at 7:58
1 Answer
Reset to default 1A 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.