2

Is it possible to use the jQuery library server side in a Doc or Sheet Container Bound Google Apps Script? If so, how?

In this question, I asked how to use jQuery in a container bound google apps script. The answer was correct, but was for the client side. I am interested in using utility functions such as $.extend() in my server side code.

Community
  • 1
  • 1
Myer
  • 3,670
  • 2
  • 39
  • 51
  • I don't see why this question it's "too broad", I can easily answer it. It seems that the guys that closed it do not know what's Google Apps Script. – Henrique G. Abreu Jun 23 '14 at 01:42
  • I agree with Henrique Abreu's comment.Please reopen. – Serge insas Jun 23 '14 at 05:42
  • `.gs` code can be put into multiple files. A function from one `.gs` file can call a function in another `.gs` file. If you could copy the jQuery code that you would normally refer to in a script tag on the front end, to a `.gs` file, maybe there would be a way to do it that way? It would be interesting if someone found a way to do it. – Alan Wells Jun 26 '14 at 03:44

1 Answers1

0

I've thought a bit about this, but haven't had a chance to try it out yet. In case it helps others, here are some thoughts that might get someone going in the right direction.

The first step is how to bring the code into GAS. Here is an older blog post from a Google DevRel on how to include Underscore on the container side. The basic idea is copy/pasting the JS library into a GAS library. Then referencing that GAS library in your container bound script.

http://googleappsdeveloper.blogspot.com/2012/11/using-open-source-libraries-in-apps.html

Underscore is a much smaller library of course and doesn't have the same dependencies that jQuery does. Happily jQuery 2 was updated to work without a browser on the server side for at least Node. Here are the steps for using it with Node. Note in Node it depends on htmlparser and jsdom. In GAS we have XmlService, which I don't think we can count on having the same JS structure as jsdom objects. You may have to bring in jsdom as well (along with its dependencies), or maybe write a jsdom wrapper over XMLService.

http://www.hacksparrow.com/jquery-with-node-js.html

Assuming step 2 can be made to work, there may still be jQuery dependency issues. You probably don't want/need all of jQuery. To reduce dependencies you could look at building your own trimmed down version. Here are some instructions for doing that. You could remove ajax for example with grunt custom:-ajax.

https://www.npmjs.com/package/jquery

Hope that helps or at least makes it clear why this is hard :).

studgeek
  • 14,272
  • 6
  • 84
  • 96