通过Apps-Script获取容器绑定的Google Apps-Script的JSON或下载 [英] Get JSON of container-bound Google Apps-Script through Apps-Script or download

查看:57
本文介绍了通过Apps-Script获取容器绑定的Google Apps-Script的JSON或下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If you create a non-container bound g-apps script (i.e. not as part of a gDoc or a gSheet), you can download it (however not view as a .json directly in the browser from the link) from gDrive as a .json. If you download a gDoc or gSheet, it converts to xlsx or docx and opening these with a zip viewer shows a number of files (many of type xml) however none contain the Google version's attached scripts.

  1. 是否可以从另一个Google Apps中以.json格式读取脚本文件 脚本?也许使用Drive-API或g-a-s. DriveApp类?

  1. Is there a way to read script files as a .json from within another Google Apps Script? perhaps using the Drive-API or with g-a-s. DriveApp class?

  1. 是否可以通过DriveApp下载或读取驱动器的.jsons 容器绑定的脚本(通常在原始gFile中对于所有脚本都是不可见的)?
  1. Is there a way to download or read through DriveApp, the .jsons of container bound scripts (which are usually invisible from all but within the original gFile)?

更新

Update

基于Kriggs,添加了Logger.log(link),这非常适合独立脚本.

Based on Kriggs, added a Logger.log(link) and this works great for stand-alone scripts.

容器绑定怎么样?

对于独立脚本文件:

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

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

for container-bound script files, there are links to csv, sheet and pdf, but no script 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 
    }

更新

Update

在Google工作表中,转到工具"->脚本编辑器"-> 地址栏中的网址如下:

In Google sheet, go to Tools->script Editor-> URL in address bar looks like:

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

这是下载json:

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

问题是,我们可以使用Drive API查找[ProjectKey]

Question is, can we use the Drive API to find [ProjectKey]

是否有对DriveApp/Drive-API方法的功能请求,以在您的帐户中寻找项目密钥?

Have there been any feature requests for DriveApp/Drive-API methods to seek Project Keys in your account?

是否有一种方法可以测试文件是否具有容器绑定脚本?然后的问题是,文件大小中是否包含脚本(可以很容易地对其进行测试,但是对于提问者而言,这是未知的.)

Would there be a way to test if a file has a container bound script? Then the question is, is the script included in the file size (this can be easily tested, however it is unknown to the asker at this point).

尽管在计算上看起来很昂贵,但类似的事情可能会起作用:

Something like this may work although it looks computationally costly:

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

推荐答案

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

Yes you have to get it trough the Drive API with OAuth2, I used the DriveApp to get the fileId, but you can modify to use Drive api aswell. To enable the Drive API go to Resources -> Advanced Google Services, find the Drive API and turn on.

通过云端硬盘发送get时,您将获取包含属性exportLinks的文件的对象,并使用该对象通过OAuth2身份验证(ScriptApp.getOAuthToken())来获取URL,所获取的字符串将为JSON,其中的数组files具有脚本的集合.

When you send a get with Drive you get back an object of the file which contains the property exportLinks, using it you fetch the URL with OAuth2 authentication (the ScriptApp.getOAuthToken()), the fetched string will be a JSON, which has the Array fileswith the colection of scripts.

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. 找不到驱动器API的名称
  5. 没有从Drive API或DriveApp返回的对象引用脚本

我想这几乎是隐身的,怀疑是否有ATM的出路.

I guess it is pretty much incognito, doubt there's any way ATM.

您始终可以制作一个独立应用,并将其设置为电子表格的库...

You can always make a Standalone app and set it as a library for the Spreadsheet...

这篇关于通过Apps-Script获取容器绑定的Google Apps-Script的JSON或下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆