vrijdag 7 februari 2020

Accessing Google Apps Scripts (as such) and (re)using them as scripts

For a script I needed a list of (some) other scripts of the same Google Apps Script project. I started to google and as almost always I found an answer at Stack Overflow (this is the answer).

The second question that needed an answer was how to (re)use the listed scripts as scripts. I continued to google and found the (beginning of) an answer here.

It works!

Updated 09-02-2020

function scriptsToArrayToScripts(){

var scripts = [];

  for(k in this){
  
    if(/something/.test(k)){
    
      scripts.push([k,this[k]]);
         
    }
  }
  
  for(var s=0;s<scripts.length;s++){


  var paramOne = "abc";
  var paramTwo = "xyz";
  
     if(scripts[s][0] == "somethingelse"){
  
        scripts[s][1](paramOne,paramTwo);
      
      }
   }   
}

woensdag 5 februari 2020

Using GAS Logger for 'creating' variables

Some times information of a file or folder is needed in a script, for example an ID or whatever.  Using Google Apps Script Logger is an easy way to get that information.

(The existence of the folder or file and a unique name of the folder or file is required)

Example one:

function useLoggerToCreateVariablesOne(){

var array = [];

var files = DriveApp.searchFiles('title = "AAA"');
while (files.hasNext()) {
  var file = files.next();
  array.push('var ID_AAA = "' + file.getId() + '"; \n');
}

var folders = DriveApp.searchFolders('title = "BBB"');
while (folders.hasNext()) {
  var folder = folders.next();
  array.push('var ID_BBB = "' + folder.getId()  + '"; \n');
}

var folders = DriveApp.searchFolders('title = "CCC"');
while (folders.hasNext()) {
  var folder = folders.next();
  array.push('var ID_CCC = "' + folder.getId()  + '"; \n\n');
}
  
var myEmail = Session.getActiveUser();
  array.push('var MY_EMAIL = "' + myEmail  + '"; \n');


Logger.log(array.toString().replace(/,/g,""))

/*
[20-02-05 08:42:02:163 CET] var ID_AAA = "aaaaa"; 
var ID_BBB = "bbbbb"; 
var ID_CCC = "ccccc"; 

var MY_EMAIL = "xxx@xxxxxxxxxx.nl"; 
*/

//Copy and past (somewhere else):

var ID_AAA = "aaaaa"; 
var ID_BBB = "bbbbb"; 
var ID_CCC = "ccccc"; 

var MY_EMAIL = "xxx@xxxxxxxxxx.nl";

}


Example two:

function useLoggerToCreateVariablesTwo(){

var query = "mimeType contains 'application/vnd.adobe.xfdf' and title contains '_form'";

var file = DriveApp.searchFiles(query).next();
var fileId = file.getId();Logger.log(file.getName())
var fileContent = file.getBlob().getDataAsString();

var modified = fileContent.split(/modified="/)[1].split(/"/)[0].toString();
var original = fileContent.split(/original="/)[1].split(/"/)[0].toString();
var formName = fileContent.split(/f href="/)[1].split(/"/)[0].toString();

Logger.log('var MODIFIED = "' + modified + '";\n' + 'var ORIGINAL = "' + original + '";\n' + 'var FORMNAME = "' + formName + '";');

DriveApp.getFileById(fileId).setTrashed(true);

/*
[20-02-05 08:26:43:771 CET] var MODIFIED = "xxxxx";
var ORIGINAL = "yyyyy";
var FORMNAME = "zzzzz.pdf";
*/

//Copy and past (somewhere else):

var MODIFIED = "xxxxx";
var ORIGINAL = "yyyyy";
var FORMNAME = "zzzzz.pdf";
}