7

I'm looking for a way to read the contents of the script.gs files within a standalone Google Apps Script. I can't find a suitable getAs() Mime format in the Docslist.File service that doesn't fail. The context of this is that I want to use the html service/content service to be able to embed public GAS source samples in Google Sites and elsewhere. (in much the same way as embedding Gist Samples). Any ideas?

bruce
  • 1,408
  • 11
  • 33

2 Answers2

16

It is, in fact, possible. This gets Code.gs:

function myFunction() {
  throw ScriptApp.getResource("Code").getDataAsString();
}
Corey G
  • 7,754
  • 1
  • 28
  • 28
  • 1
    I've implemented this now. Works great. http://excelramblings.blogspot.co.uk/2012/11/google-apps-script-new-day-new-feature.html – bruce Nov 05 '12 at 18:04
  • There is a "GAS Library Management" that implements the Corey's solution: https://sites.google.com/site/nnillixxsource/Roberts/RoseCMMSv020/global/getCode_js – Juan Lanus Aug 15 '14 at 15:46
  • 2
    I think this functionality has been removed. I cannot find it anymore. Is there an alternative? – user1933178 Jul 12 '20 at 12:14
  • This doesn't appear to be documented anywhere, or included in the Typescript type definition for `ScriptApp`, but I just tested this in an Apps Script project and it does work (Aug 2023) – mark.monteiro Aug 03 '23 at 20:02
-2

There is no way to programmatically retrieve the contents of a ".gs" file. You can only ask for files with ".html" extension and potentially you can return it in a project.

This request has come up in the context of allow local development or Git integration to provide an API way to get/set script source in a file. Please put your comments in this issue tracker request or log a new one if it makes sense .

http://code.google.com/p/google-apps-script-issues/issues/detail?id=1108

Arun Nagarajan
  • 5,547
  • 1
  • 22
  • 19
  • 1
    Thank you. It seems that the request you linked to is moe abou being able to update apps script code using some version control system. I can see this might be useful, but my request,is a little simpler. I just want to be able to read the contents of a script. Thanks for the pointer though. I will raise a,separate rfq. – bruce Nov 04 '12 at 00:27