简体   繁体   中英

How to get text content of Google Apps Script?

the objective is to get all files of SCRIPT type from Google Drive; & in a loop get their text contents one by one.

My code is:

function searchFiles() {
  //https://developers.google.com/apps-script/reference/base/mime-type
  var type = MimeType.GOOGLE_APPS_SCRIPT;
  var files = DriveApp.getFilesByType(type);
  while (files.hasNext()) {
    var file = files.next();
    process(file);
  }
}
function process(file){
  //https://developers.google.com/apps-script/reference/base/mime-type
  var txt = file.getBlob(PLAIN_TEXT);
  Logger.log(txt);
}

The error I get from file.getBlob() is: Converting from application/vnd.google-apps.script to application/pdf is not supported.

I have also tried the FetchURL for file.getUrl() but that too just gives the whole HTML for file if I open in same window as I am logged in & Error Page HTML if opened in incognito.

Is it possible to get text contents of a script file in Google Apps Script?

Flow:

  • Get all script files by MIME
  • Get id of each file
  • Urlfetch it by Drive REST API or Apps Script API

Script:

function getScriptSourceCode(fileid) {
  var params = {
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
    followRedirects: true,
    muteHttpExceptions: true,
  };
  var url =
    'https://script.google.com/feeds/download/export?id=' +
    fileid +
    '&format=json';
  var response = UrlFetchApp.fetch(url, params);
  var json = JSON.parse(response);
  for (var file in json['files']) {
    Logger.log(json['files'][file]['source']); //Source code
  }
}

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM