Dav*_*ana 1 javascript text google-apps-script
目标是从 Google Drive 获取所有 SCRIPT 类型的文件;& 循环中一一获取它们的文本内容。
我的代码是:
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);
}
Run Code Online (Sandbox Code Playgroud)
我从 file.getBlob() 得到的错误是:Converting from application/vnd.google-apps.script to application/pdf is not supported.
我也尝试过 FetchURL,file.getUrl()
但如果我在登录时在同一窗口中打开,那么它也只会给出文件的整个 HTML,如果在隐身模式中打开,则会给出错误页面 HTML。
是否可以在 Google Apps 脚本中获取脚本文件的文本内容?
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
}
}
Run Code Online (Sandbox Code Playgroud)