通过Apps-Script获取容器绑定的Google Apps脚本的JSON或下载

时间:2015-07-02 10:20:10

标签: json google-apps-script google-drive-api google-docs

如果您创建一个非容器绑定的g-apps脚本(即不是gDoc或gSheet的一部分),您可以从gDrive下载它(但不能直接在浏览器中从链接查看.json)作为.json。如果您下载了gDoc或gSheet,它会转换为xlsx或docx,并使用zip查看器打开这些文件会显示许多文件(许多类型为xml),但是没有包含Google版本的附加脚本。

  1. 有没有办法从另一个Google Apps中将脚本文件作为.json读取 脚本?也许使用Drive-API或g-a-s。 DriveApp类?

    1. 有没有办法下载或阅读DriveApp,.jsons 容器绑定脚本(通常在原始gFile中不可见)?
  2. 更新

    基于Kriggs,添加了Logger.log(link),这对于独立脚本非常有用。

    容器绑定怎么样?

    对于独立脚本文件:

       exportLinks={
    application/vnd.google-apps.script+json=
    script.google.com/feeds/download/export?id=[scriptId]&format=json
    }
    

    对于容器绑定的脚本文件,有指向csv,sheet和pdf的链接,但没有脚本json。

     exportLinks= { 
        text/csv=docs.google.com/spreadsheets/export?id=[sheetId]&exportFormat=csv, 
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet=
        docs.google.com/spreadsheets/export?id=[sheetId]exportFormat=xlsx, 
    
        application/pdf=
        docs.google.com/spreadsheets/export?id=[sheetId]&exportFormat=pdf 
        }
    

    更新

    在Google表格中,转到工具 - >脚本编辑器 - > 地址栏中的URL如下所示:

        https://script.google.com/macros/d/
    [ProjectKey]/edit?uiv=2&mid=[aVeryLongAlphaNum]
    

    这是json的下载:

    https://script.google.com/feeds/download/export?id=[ProjectKey]
    

    问题是,我们是否可以使用云端硬盘API查找[ProjectKey]

    是否有任何功能请求使用DriveApp / Drive-API方法在您的帐户中搜索项目密钥?

    是否有办法测试文件是否容器绑定脚本?那么问题是,脚本是否包含在文件大小中(这可以很容易地进行测试,但此时提问者不知道。)

    这样的事情可能有用,虽然看起来计算成本很高:

    var SizeOfFile = yourFile.getSize();//
    var charsInFile = yourFile.getAsString();
    var unicodeSizeReference = [];//get bytes per character array
    charsInFile.sort()
    //find frequency of characters then multiply from unicoseSizeReference.
    //there could be other gotchas as well, however this is just testing for feasibility
    var SizeOfTextInFile = [/*#of chars in file name and sheetname*/]+[/*#of chars in all sheets*/];
    SizeOfTextInFile *= unicodeBytesPerCharacter;//ranges from 1 to 4
    
    var someThreshold = 10;//bytes
    var hasScript=0;
    if ([SizeOfFile - SizeOfTextInFile] > someThreshold) hasScript=1
    

1 个答案:

答案 0 :(得分:1)

是的,您必须通过OAuth2的Drive API获取它,我使用DriveApp获取fileId,但您可以修改以使用Drive api。要启用Drive API,请转到资源 - >高级Google服务,找到Drive API并开启。

当您使用云端硬盘发送get时,您会返回包含属性exportLinks的文件对象,使用它来获取带有OAuth2身份验证的URL(ScriptApp.getOAuthToken()),获取的字符串将是一个JSON,它具有带有脚本集合的数组files

function getAppsScriptAsJson( fileName ) {
  var fileDrive = Drive.Files.get( DriveApp.getFilesByName( fileName ).next().getId() );
  var link = JSON.parse(fileDrive)[ 'exportLinks' ][ 'application/vnd.google-apps.script+json' ];
  var fetched = UrlFetchApp.fetch(link, {headers:{'Accept':'application/vnd.google-apps.script+json', "Authorization":'Bearer '+ScriptApp.getOAuthToken()}, method:'get'});
  return JSON.parse(fetched.getContentText());
}

至于容器绑定:

  1. DriveApp无法按名称获取
  2. 它不会在任何地方显示ID,只显示项目密钥
  3. Drive API无法按项目ID进行查找,也无法按照DriveApp
  4. 进行查找
  5. 无法通过名称找到Drive API
  6. Drive API和DriveApp
  7. 中没有来自返回对象的脚本引用

    我想这几乎是隐姓埋名的,怀疑ATM有什么办法。

    您可以随时制作独立应用并将其设置为电子表格的库...