Copy/rename google sheet, share & get share id with script in form -


is possible script copy/rename (unrelated) google spreadsheet, share given email (preferably testing existence of google account associated given email first), , save shared url? have spent hours , can't find part of answer this: nothing on copying/renaming, nothing on share id. may missing keyword or something. realize i'll downvoted general question, google script support out there, think. if give me thread, i'll follow it.

so: had figure out, , hired code snippet me, here's got. works pretty great me. did tiny bit of customization.

here's requirement: given 2 google sheets, gtemplate & gtracker (these 2 separate spreadsheets, not tabs in same spreadsheet). create form (that reports tab/sheet in gtracker) user (anyone) fills in online email "useremail", , string "userid". upon submission: 1) make copy of gtemplate, , rename "gtuserid" (if gtuserid exists, name gtuserid1, 2, or whatever) 2) check submitted email has associated google account 3) share gtuserid useremail 4) save url of gtuserid gtracker 5) send email useremail confirming success or failure of above

    //************ edit here *******************************      var stremplateid = '1pbo9khoza9ix3uik-fxxgnvhgs0bonqujmv95uug56o';      //******************************************************    function onopen() {   var ui = spreadsheetapp.getui();    ui.createmenu('initialization')       .additem('initialize', 'settrigger')       .addtoui(); }   function settrigger(){  var arr=   scriptapp.getprojecttriggers();  (var i= arr.length; i>0; i--){ scriptapp.deletetrigger(arr[i-1]) }    scriptapp.newtrigger('onformsubmit')           .forspreadsheet(spreadsheetapp           .getactivespreadsheet().getid())           .onformsubmit()           .create() }     function onformsubmit(e) {      try {          //logger.log(json.stringify(e))          var folder;         var stremail = e.namedvalues.email;         var struid = e.namedvalues['user id'];         //logger.log(stremail)         //logger.log(struid)          var oldfile = driveapp.getfilebyid(stremplateid);         var folders = oldfile.getparents();         while (folders.hasnext()) {                 folder = folders.next();                 break;         }              if ((typeof folder) != "object") {                 folder = driveapp.getrootfolder();             }              var bolflag = false;             var bolflag1 = false;             var = 0;              var myregexp = new regexp('[^a-za-z0-9.-]','g');             var strprocuid=string(struid).replace(myregexp, "_");             //logger.log(strprocuid)              var strfilename = strprocuid;             while (!bolflag) {                 var files = folder.getfiles();                 while (files.hasnext()) {                     var file = files.next();                     if (file.getname() == strfilename) {                         bolflag1 = true;                     }                 }                  if (!bolflag1) {                     bolflag = true;                 } else {                     = + 1;                     strfilename = strprocuid + i;                     bolflag1 = false;                 }             }              var newfile = oldfile.makecopy(strfilename, folder);             newfile.addeditors(stremail);              var link = newfile.geturl();          var sh = spreadsheetapp.getactivespreadsheet().getsheets()[0];         var row = sh.getlastrow();         var col = sh.getlastcolumn();          sh.getrange(row, col, 1, 1).setvalue(link);          var body = 'dear ' + struid + ',\n' +             'your request has been processed successfully.\n' +             'the file can seen here:\n' +             link + '\n' +             'regards,\n ' +             'admin';              gmailapp.sendemail(stremail, 'request processed', body);      } catch (e) {          var body = '\n' +             'an error occurred while processing request:\n' +             'user id: ' + struid + '\n ' +             'email: ' + stremail + '\n ' +             'error: ' + e;              gmailapp.sendemail(session.geteffectiveuser().getemail(), 'error processing request', body);              var body = 'dear ' + struid + ',\n' +             'sorry, error occurred while processing request.\n' +             'regards,\n ' +             'admin';              gmailapp.sendemail(stremail, 'error processing request', body);      } } 

perhaps helpful else. things couldn't find copying copied/shared google sheet url different sheet (for creating daughter shared documents different projects, initiated project teams, still owned admin account, internal fields readily accessible since we've got url). hope that's clear , helpful.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -