如何获取 Google Apps Script 的文本内容? [英] How to get text content of Google Apps Script?
问题描述
目标是从 Google Drive 获取所有 SCRIPT 类型的文件;&在循环中一一获取他们的文本内容.
the objective is to get all files of SCRIPT type from Google Drive; & in a loop get their text contents one by one.
我的代码是:
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);
}
我从 file.getBlob() 得到的错误是:不支持从 application/vnd.google-apps.script 转换为 application/pdf.
The error I get from file.getBlob() is: Converting from application/vnd.google-apps.script to application/pdf is not supported.
我也尝试过 file.getUrl()
的 FetchURL,但是如果我在登录时在同一窗口中打开,它也只会提供文件的整个 HTML如果以隐身方式打开,则会出现错误页面 HTML.
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.
是否可以在 Google Apps 脚本中获取脚本文件的文本内容?
Is it possible to get text contents of a script file in Google Apps Script?
推荐答案
流程:
- 通过 MIME 获取所有脚本文件
- 获取每个文件的id
- 通过 Drive REST API 或 Apps Script API 进行 Urlfetch
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
}
}
参考:
- 导出项目
- Apps 脚本 API 立>
- 授权范围
这篇关于如何获取 Google Apps Script 的文本内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!