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";
}

Geen opmerkingen:

Een reactie posten