Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJS with Google Apps Script

I am wondering if it is possible to use RequireJS with Google Apps Script. I am loading RequireJS 2.1.5 from a GAS library. This sample app should log the word Monkey but nothing happens when I test it. No errors or warnings are being generated either.

index.html:

<!doctype html>
<head>
  <title></title>
</head>
<body>
  <script data-main="main" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.5/require.js"></script>
</body>
</html>

main.gs:

var define = Require.loadDefine();
var require = Require.loadRequire();

define( function() {
  Logger.log("Monkey");
});

code.gs:

function doGet(e) {
  Logger.log("doGet");
  return HtmlService.createHtmlOutputFromFile('index').setSandboxMode(HtmlService.SandboxMode.NATIVE); 

  var t;
  t = HtmlService.createTemplateFromFile('index');
  return t.evaluate();
}
like image 697
user2514982 Avatar asked Apr 24 '26 11:04

user2514982


1 Answers

I found the answer while reading JavaScript Module Pattern: In-Depth

The code inside the module didn't have access to Logger. Passing this as a parameter makes it work.

define( function(g) {
  g.Logger.log("Monkey");
}(this));
like image 56
user2514982 Avatar answered Apr 27 '26 09:04

user2514982



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!