5

Please let me know how to inject external javascript file in google app script(Google Docs).

Thanks in advance.

Prashant Mhase
  • 111
  • 3
  • 6
  • Possible duplicate of [importing external javascript to google apps script](http://stackoverflow.com/questions/18646554/importing-external-javascript-to-google-apps-script) – owyongsk Apr 12 '17 at 08:06
  • Related: [How to load javascript from external source and execute it in Google Apps Script](https://stackoverflow.com/q/15548124/1595451) – Rubén May 02 '19 at 07:45

2 Answers2

14

Depending on what you mean by Inject, one possible answer is to use the standard javascript eval() function along with UrlFetchApp:

eval(UrlFetchApp.fetch('http://path.to/external/javascript.js').getContentText());

If you want to include a JS file in the HTML output of a published script, simply include the javascript using tags in your HTML.

<script src="http://path.to/external/javascript.js"></script>

Eval docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval UrlfetchApp docs: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32
  • 1
    I have tried it by using the eval() and UrlFetchApp but didn't work for me because my external js uses the HTML window and document object. I am getting the **Error undefined window object** – Prashant Mhase Nov 05 '14 at 05:21
  • 1
    That makes sense, within Apps Script there is no concept of the DOM objects like window and document, because these represent the client-side browser, and Apps Script runs in a server-side environment. – Cameron Roberts Nov 05 '14 at 14:37
  • i cant load jquery with 'script' tag on client side, something related to "strict-dynamic" specified – Dee May 06 '19 at 07:40
  • 1
    @datdinhquoc I believe the caja sanbox limits you to older versions of jquery, try version 3.2.1 – Cameron Roberts May 06 '19 at 14:41
  • it loads ok now, what i saw were just yellow warnings – Dee May 07 '19 at 02:50
1

Do you expect the Javascript file you wish to "inject" to change regularly? If not, the easiest way will be to make a new script file in your project, and add the content of the file you want to inject. For instance, let's say you start with Code.gs with just one function, based heavily on the Spreadsheets project template:

/**
 * Adds a custom menu to the active spreadsheet
 */
function onOpen() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Tell a Joke",
    functionName : "beFunny"
  }];
  spreadsheet.addMenu("Script Center Menu", entries);
};

You see I'm calling "beFunny()" which is not in this file. Instead, it's in a new file, ExtraStuff.gs:

function beFunny() {
  Browser.msgBox('Waka waka waka');
}

Run this, and the Tell a Joke menu item works, even though nothing in Code.gs refers to the existence of another script file. Instead, the functions and variables declared in of all of the files in the project are "in scope" for one another.

Jesse Scherer
  • 1,492
  • 9
  • 26
  • I want to insert/load/inject the my external js file into the google docs main HTML page. – Prashant Mhase Nov 05 '14 at 05:26
  • 1
    If you want to add JavaScript to Google Docs' HTML, then Apps Script may not be what you want. There are tools like [Tampermonkey](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=en) for Chrome and [Firebug](http://getfirebug.com/) for Firefox which will let you add Javascript to any page you visit. It sounds like this may better meet your needs. – Jesse Scherer Nov 05 '14 at 12:13
  • 1
    I suggest asking a fresh question that more directly describes what you want to do. Google doesn't provide any direct mechanism for manipulating the interface of Google Drive, however there are other techniques that could work (like those that jjjjoe mentioned, and others) depending on what it is you are trying to do. Bookmarklets come to mind, they allow you to add Javascript to the currently open page by clicking a Bookmark in the browser. – Cameron Roberts Nov 05 '14 at 14:39