简体   繁体   中英

Get JSON of container-bound Google Apps-Script through Apps-Script or download

If you create a non-container bound g-apps script (ie 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. Is there a way to read script files as a .json from within another Google Apps Script? perhaps using the Drive-API or with gas. DriveApp class?

    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

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

How about for container-bound?

for stand alone script files:

   exportLinks={
application/vnd.google-apps.script+json=
script.google.com/feeds/download/export?id=[scriptId]&format=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

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]

this is the download json:

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

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

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

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.

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 files with 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());
}

As for container bound:

  1. DriveApp can't get it by name
  2. It doesn't display an ID anywhere, just the project key
  3. Drive API can't lookup by the project id, nor DriveApp
  4. Drive API can't find by the name
  5. There's no reference of the script from the returned object from Drive API nor the DriveApp

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...

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