google apps script - ListBox will not populate with new API -
for reason when run app runs except listbox not populate. tried change while looking thinking interator reason listbox not populate folders have in drive. appreciated, thanks!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /* template generator by: andre fecteau - klutch2013@gmail.com original code from: kiszal@gmail.com (found in template gallery searching "templates" first one. major from: serge insas on stack overflow (he did of work.) link 1: http://stackoverflow.com/questions/18147798/e-undefined-google-script-error link 2: http://stackoverflow.com/questions/18132837/have-a-listbox-populate-with-every-folder-in-mydrive how use: first: each column designated in template {column letter} example column {a} second: can change this, template must in folder called "templates." folder can anywhere in drive. third: click "generate documents here!" click "export row document" fourth: type in row want export. chose folder path. click submit. note on fourth step: if want number skip header row add +1 line 32. mean if typed "2" in row box exports row 3. took out because can confusing @ times. note: line 71 can edit word "templates" whatever folder saved template into. note: line 36 can edit change comes in name of file created. edit column letter in following piece: "+sheet.getrange('e'+row).getvalue()+" can delete other columns not want deleteing section of code looks following: '+sheet.getrange('d'+row).getvalue()+' feel free edit code wish , needs. did original code. there no reason should restrict others code. bug fix log: * changed row: 32 remove +1 6/18/2014 * changed row: 40 remov e row-- 6/18/2014 * changed row: 68 update driveapp because of google api update - 4/21/15 * changed row: 68 update getfoldersbyname , getfilesbytype update new google api - 4/21/15 */ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function generatedocument(e) { var template = driveapp.getfilebyid(e.parameter.templates); logger.log(template.getname()); var sheet = spreadsheetapp.getactivespreadsheet(); var row = number(e.parameter.row) //+1; // remove // in line next +1 skip headers logger.log(row); var currentfid = e.parameter.curfid; logger.log(currentfid); var mydocid = template.makecopy(sheet.getrange('b' + row).getvalue() + ' - ' + sheet.getrange('e' + row).getvalue() + ' - ' + sheet.getrange('d' + row).getvalue() + ' - ' + sheet.getrange('x' + row).getvalue()).getid(); var mydoc = documentapp.openbyid(mydocid); var copybody = mydoc.getactivesection(); var sheet = spreadsheetapp.getactivespreadsheet(); //row--; // decrement row number in concordance real row numbers in sheet var myrow = spreadsheetapp.getactivespreadsheet().getrange(row + ":" + row); (var = 1; < sheet.getlastcolumn() + 1; i++) { var mycell = myrow.getcell(1, i); copybody.replacetext("{" + mycell.geta1notation().replace(row, "") + "}", mycell.getvalue()); } mydoc.saveandclose(); var destfolder = driveapp.getfolderbyid(currentfid); logger.log(mydocid); var doc = driveapp.getfilebyid(mydocid); // document again using driveapp time... doc.addtofolder(destfolder); // add desired folder doc.removefromfolder(driveapp.getrootfolder()); // did step step more easy follow var pdf = driveapp.getfilebyid(mydocid).getas("application/pdf"); destfolder.createfile(pdf); // create pdf file in folder var app = uiapp.getactiveapplication(); app.close(); return app; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function gettemplates() { var doc = spreadsheetapp.getactivespreadsheet(); var app = uiapp.createapplication().settitle('generate template'); // create grid 3 text boxes , corresponding labels var grid = app.creategrid(5, 2); grid.setwidget(0, 0, app.createlabel('template name:')); var list = app.createlistbox(); list.setname('templates'); grid.setwidget(0, 1, list); var foldertemplate = driveapp.getfoldersbyname("templates"); while (foldertemplate.hasnext()) { var folder = foldertemplate.next(); logger.log(folder.getname()); }; var allmyfilesbytype = folder.getfilesbytype(mimetype.google_docs); while (allmyfilesbytype.hasnext()) { var file = allmyfilesbytype.next(); logger.log(file.getname()); }; grid.setwidget(1, 0, app.createlabel('row:')); var row = app.createtextbox().setname('row'); row.setvalue(spreadsheetapp.getactivespreadsheet().getactiverange().getrow()); grid.setwidget(1, 1, row); var curfn = app.createtextbox().settext('mydrive/').setname('curfn').setid('curfn').setwidth('400'); var curfid = app.createtextbox().settext(driveapp.getrootfolder().getid()).setname('curfid').setid('curfid').setvisible(false); var listf = app.createlistbox().setname('listf').setid('listf').additem('please select folder', 'x'); grid.settext(2, 0, 'type path:').setwidget(2, 1, curfn).settext(3, 0, 'or').settext(4, 0, 'choose path:').setwidget(4, 1, listf).setwidget(3, 1, curfid); var folders = driveapp.getfolders(); while (folders.hasnext()) { var folder = folders.next(); logger.log(folder.getname()); }; var handlerf = app.createserverhandler('folderselect').addcallbackelement(grid); listf.addchangehandler(handlerf); var panel = app.createverticalpanel(); panel.add(grid); var button = app.createbutton('submit'); var handler = app.createserverclickhandler('generatedocument'); handler.addcallbackelement(grid); button.addclickhandler(handler); // add button panel , panel application, display application app in spreadsheet doc panel.add(button); app.add(panel); doc.show(app); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function folderselect(e) { var app = uiapp.getactiveapplication(); var currentfn = e.parameter.curfn; var currentfid = e.parameter.listf; logger.log(currentfid); var listf = app.getelementbyid('listf'); var curfn = app.getelementbyid('curfn'); var curfid = app.getelementbyid('curfid'); if (currentfid == 'x') { currentfid = driveapp.getrootfolder().getid(); curfn.settext('mydrive/') }; var startfolder1 = driveapp.getfolderbyid(currentfid); while (folders.hasnext()) { var folder = startfolder1.next(); logger.log(folder.getname()); }; var startfolder = driveapp.getfolders(); while (startfolder.hasnext()) { var folder = folders.next(); logger.log(folder.getname()); }; listf.clear().additem('no more sub folders!', 'x').additem('go root', 'x'); if (folders.length > 0) { listf.clear(); listf.additem('select sub folder', 'x') }; (var = 0; < startfolder.length; i++) { listf.additem(folders[i].getname(), folders[i].getid()) } curfn.settext(currentfn + driveapp.getfoldersbyid(currentfid).getname() + '/'); if (currentfid == driveapp.getrootfolder().getid()) { curfn.settext('mydrive/') }; curfid.settext(currentfid); return app; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function onopen() { var ss = spreadsheetapp.getactivespreadsheet(); var menuentries = [{ name: "export row document", functionname: "gettemplates" }]; ss.addmenu("generate documents here!", menuentries); }
here happening:
problem when opening spreadsheet ui not loading folder name listbox listf. here did......
while (folders.hasnext())
{ var folder = folders.next(); logger.log(folder.getname()); };
you need add statement this
listf.additem(folder.getname(),folder.getid())
list box populated try code.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /* template generator by: andre fecteau - klutch2013@gmail.com original code from: kiszal@gmail.com (found in template gallery searching "templates" first one. major from: serge insas on stack overflow (he did of work.) link 1: http://stackoverflow.com/questions/18147798/e-undefined-google-script-error link 2: http://stackoverflow.com/questions/18132837/have-a-listbox-populate-with-every-folder-in-mydrive how use: first: each column designated in template {column letter} example column {a} second: can change this, template must in folder called "templates." folder can anywhere in drive. third: click "generate documents here!" click "export row document" fourth: type in row want export. chose folder path. click submit. note on fourth step: if want number skip header row add +1 line 32. mean if typed "2" in row box exports row 3. took out because can confusing @ times. note: line 71 can edit word "templates" whatever folder saved template into. note: line 36 can edit change comes in name of file created. edit column letter in following piece: "+sheet.getrange('e'+row).getvalue()+" can delete other columns not want deleteing section of code looks following: '+sheet.getrange('d'+row).getvalue()+' feel free edit code wish , needs. did original code. there no reason should restrict others code. bug fix log: * changed row: 32 remove +1 6/18/2014 * changed row: 40 remov e row-- 6/18/2014 * changed row: 68 update driveapp because of google api update - 4/21/15 * changed row: 68 update getfoldersbyname , getfilesbytype update new google api - 4/21/15 */ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function generatedocument(e) { var template = driveapp.getfilebyid(e.parameter.templates); logger.log(template.getname()); var sheet = spreadsheetapp.getactivespreadsheet(); var row = number(e.parameter.row) //+1; // remove // in line next +1 skip headers logger.log(row); var currentfid = e.parameter.curfid; logger.log(currentfid); var mydocid = template.makecopy(sheet.getrange('b' + row).getvalue() + ' - ' + sheet.getrange('e' + row).getvalue() + ' - ' + sheet.getrange('d' + row).getvalue() + ' - ' + sheet.getrange('x' + row).getvalue()).getid(); var mydoc = documentapp.openbyid(mydocid); var copybody = mydoc.getactivesection(); var sheet = spreadsheetapp.getactivespreadsheet(); //row--; // decrement row number in concordance real row numbers in sheet var myrow = spreadsheetapp.getactivespreadsheet().getrange(row + ":" + row); (var = 1; < sheet.getlastcolumn() + 1; i++) { var mycell = myrow.getcell(1, i); copybody.replacetext("{" + mycell.geta1notation().replace(row, "") + "}", mycell.getvalue()); } mydoc.saveandclose(); var destfolder = driveapp.getfolderbyid(currentfid); logger.log(mydocid); var doc = driveapp.getfilebyid(mydocid); // document again using driveapp time... doc.addtofolder(destfolder); // add desired folder doc.removefromfolder(driveapp.getrootfolder()); // did step step more easy follow var pdf = driveapp.getfilebyid(mydocid).getas("application/pdf"); destfolder.createfile(pdf); // create pdf file in folder var app = uiapp.getactiveapplication(); app.close(); return app; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function gettemplates() { var doc = spreadsheetapp.getactivespreadsheet(); var app = uiapp.createapplication().settitle('generate template'); // create grid 3 text boxes , corresponding labels var grid = app.creategrid(5, 2); grid.setwidget(0, 0, app.createlabel('template name:')); var list = app.createlistbox(); list.setname('templates'); grid.setwidget(0, 1, list); var folders = driveapp.getfoldersbyname("templates"); while (folders.hasnext()) { var folder = folders.next(); logger.log(folder.getname()); var allmyfilesbytype = folder.getfilesbytype(mimetype.google_docs) }; while (allmyfilesbytype.hasnext()) { var file = allmyfilesbytype.next(); list.additem(file.getname()); logger.log(file.getname()); }; grid.setwidget(1, 0, app.createlabel('row:')); var row = app.createtextbox().setname('row'); row.setvalue(spreadsheetapp.getactivespreadsheet().getactiverange().getrow()); grid.setwidget(1, 1, row); var curfn = app.createtextbox().settext('mydrive/').setname('curfn').setid('curfn').setwidth('400'); var curfid = app.createtextbox().settext(driveapp.getrootfolder().getid()).setname('curfid').setid('curfid').setvisible(false); var listf = app.createlistbox().setname('listf').setid('listf').additem('please select folder', 'x'); grid.settext(2, 0, 'type path:').setwidget(2, 1, curfn).settext(3, 0, 'or').settext(4, 0, 'choose path:').setwidget(4, 1, listf).setwidget(3, 1, curfid); var folders = driveapp.getrootfolder().getfolders(); while (folders.hasnext()) { var folder = folders.next(); listf.additem(folder.getname(),folder.getid()) }; var handlerf = app.createserverhandler('folderselect').addcallbackelement(grid); listf.addchangehandler(handlerf); var panel = app.createverticalpanel(); panel.add(grid); var button = app.createbutton('submit'); var handler = app.createserverclickhandler('generatedocument'); handler.addcallbackelement(grid); button.addclickhandler(handler); // add button panel , panel application, display application app in spreadsheet doc panel.add(button); app.add(panel); doc.show(app); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function folderselect(e) { var app = uiapp.getactiveapplication(); var currentfn = e.parameter.curfn; var currentfid = e.parameter.listf; logger.log(currentfid); var listf = app.getelementbyid('listf'); var curfn = app.getelementbyid('curfn'); var curfid = app.getelementbyid('curfid'); if (currentfid == 'x') { currentfid = driveapp.getrootfolder().getid(); curfn.settext('mydrive/') }; var startfolder = driveapp.getfolderbyid(currentfid); var folders = startfolder.getfolders(); listf.clear().additem('no more sub folders!', 'x').additem('go root', 'x'); if (folders.length > 0) { listf.clear(); listf.additem('select sub folder', 'x') }; while (folders.hasnext()) { var folder = folders.next(); listf.additem(folder.getname(),folder.getid()) }; curfn.settext(currentfn + driveapp.getfolderbyid(currentfid).getname() + '/'); if (currentfid == driveapp.getrootfolder().getid()) { curfn.settext('mydrive/') }; curfid.settext(currentfid); return app; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function onopen() { var ss = spreadsheetapp.getactivespreadsheet(); var menuentries = [{ name: "export row document", functionname: "gettemplates" }]; ss.addmenu("generate documents here!", menuentries); }
Comments
Post a Comment