Trying to create multiple PDF for each row in a Google Sheet -
i trying create separate pdf each row in google sheet using google doc template. can work when use active row; however, when try loop createpdf function pdf left last row. thoughts deleting of files beforehand can't figure out error is. awesome.
var template_id = '1mxrm0kny8r7rolbou8ksuc8dovvh8pvtq-ec_nd3fuq'; function onopen() { spreadsheetapp .getui() .createmenu('create pdf') .additem('create pdf', 'createpdf') .addtoui(); } (var i=2; < 3; i++) { function createpdf() { if (template_id === '') { spreadsheetapp.getui().alert('template_id needs defined in code.gs'); return; } // set docs , spreadsheet access var copyfile = driveapp.getfilebyid(template_id).makecopy(), copyid = copyfile.getid(), copydoc = documentapp.openbyid(copyid), copybody = copydoc.getbody(), activesheet = spreadsheetapp.getactivesheet(), numberofcolumns = activesheet.getlastcolumn(), activerow = activesheet.getrange(i, 1, 1, numberofcolumns).getvalues(), headerrow = activesheet.getrange(1, 1, 1, numberofcolumns).getvalues(), columnindex = 0, filename = activerow[0][0] + activerow[0][1], pdffile, pdffile2; // replace keys spreadsheet values (;columnindex < headerrow[0].length; columnindex++) { copybody.replacetext('%' + headerrow[0][columnindex] + '%', activerow[0][columnindex]); } // create pdf file , delete doc copy copydoc.saveandclose(); pdffile = driveapp.createfile(copyfile.getas("application/pdf")); pdffile2 = pdffile.setname(filename) copyfile.settrashed(true); return pdffile2; }}
tip: pay attention formatting code properly, because understand structure. here's have after passing through jsbeautifier:
var template_id = '1mxrm0kny8r7rolbou8ksuc8dovvh8pvtq-ec_nd3fuq'; function onopen() { spreadsheetapp .getui() .createmenu('create pdf') .additem('create pdf', 'createpdf') .addtoui(); } (var = 2; < 3; i++) { function createpdf() { if (template_id === '') { spreadsheetapp.getui().alert('template_id needs defined in code.gs'); return; } // set docs , spreadsheet access var copyfile = driveapp.getfilebyid(template_id).makecopy(), copyid = copyfile.getid(), copydoc = documentapp.openbyid(copyid), copybody = copydoc.getbody(), activesheet = spreadsheetapp.getactivesheet(), numberofcolumns = activesheet.getlastcolumn(), activerow = activesheet.getrange(i, 1, 1, numberofcolumns).getvalues(), headerrow = activesheet.getrange(1, 1, 1, numberofcolumns).getvalues(), columnindex = 0, filename = activerow[0][0] + activerow[0][1], pdffile, pdffile2; // replace keys spreadsheet values (; columnindex < headerrow[0].length; columnindex++) { copybody.replacetext('%' + headerrow[0][columnindex] + '%', activerow[0][columnindex]); } // create pdf file , delete doc copy copydoc.saveandclose(); pdffile = driveapp.createfile(copyfile.getas("application/pdf")); pdffile2 = pdffile.setname(filename) copyfile.settrashed(true); return pdffile2; } } the indenting clarifies few problems:
the
forloop oniin global scope, not part of function. therefore execute every time function in script invoked, not explicitly invocable, in function.the function
createpdfdeclared insideforloop. in global scope (see point 1), it's still available called other functions such menu createdonopen(). however, because it's declaration, not invokedi loop! (this primary concern of question.)there's
returncall insidecreatepdf()left-over original copy of code produced single pdf. because returns in first time through loop, still make single pdf. line needs moved or deleted.return pdffile2;
flipping couple of lines , deleting return should sort out.
var template_id = '1mxrm0kny8r7rolbou8ksuc8dovvh8pvtq-ec_nd3fuq'; function onopen() { spreadsheetapp .getui() .createmenu('create pdf') .additem('create pdf', 'createpdf') .addtoui(); } function createpdf() { (var = 2; < 3; i++) { if (template_id === '') { spreadsheetapp.getui().alert('template_id needs defined in code.gs'); return; } // set docs , spreadsheet access var copyfile = driveapp.getfilebyid(template_id).makecopy(), copyid = copyfile.getid(), copydoc = documentapp.openbyid(copyid), copybody = copydoc.getbody(), activesheet = spreadsheetapp.getactivesheet(), numberofcolumns = activesheet.getlastcolumn(), activerow = activesheet.getrange(i, 1, 1, numberofcolumns).getvalues(), headerrow = activesheet.getrange(1, 1, 1, numberofcolumns).getvalues(), columnindex = 0, filename = activerow[0][0] + activerow[0][1], pdffile, pdffile2; // replace keys spreadsheet values (; columnindex < headerrow[0].length; columnindex++) { copybody.replacetext('%' + headerrow[0][columnindex] + '%', activerow[0][columnindex]); } // create pdf file , delete doc copy copydoc.saveandclose(); pdffile = driveapp.createfile(copyfile.getas("application/pdf")); pdffile2 = pdffile.setname(filename) copyfile.settrashed(true); } }
Comments
Post a Comment