jsPDF server side usage - google-apps-script

I really like the look of jsPDF but I can't seem to find any documentation for server side usage. The page here seems to suggest that this is possible, but the link for an example is broken or no longer exists. I would really like to use this library in a Google Apps Script project if possible.

The new home for jsPDF is here. Note that the "Server-side will work anywhere" promise is no longer presented. Chances are that it would work on Node.js, for example, but since Apps-Script is an incomplete javascript implementation it might not work there.
You can generate PDF files from within Apps-Script already. If the subject document is in Google Drive, then you can use getAs("application/pdf") to request the document as a PDF. (If it's not a Google Drive document, then first turn it into one.) Read over 4 ways to do Mail Merge using Google Apps Script, method 3, for an example.

Related

Given a script URL, how do I determine the actual document?

I was experimenting with Webextensions earlier this year connecting to scripts in my Google account, but now I cannot determine which script I was connecting to.
So I have the script URL. It looks like a normal script URL, e.g.:
https://script.google.com/macros/s/jG5dForTySatijhf-nmG49F64j62RzkgH6dFgr4SalpUyvF/dev
But I cannot work out which script that URL belongs to! Given a script URL, how do I determine the script in my Google Drive?
UPDATE:
I think I can phrase my question a little better now, and I cannot imagine I am the first person to have this problem so I imagine there must be some solution.
I have the web app URL (like the one above) for a published script. But I cannot remember which actual script (in my Google Drive) it is associated with.
Given a web app URL, how do I determine which file in my Google Drive it is associated with? I know it's one of them but I don't know how to determine which one!
Perhaps I can suggest an alternative approach. This would be particularly useful if you have been experimenting with scripts, as you suggest, and have not created a lot of projects.
All your projects can be found on Google at:
https://script.google.com/home
The metadata for each project includes the name of the container, and you open the container from that screen

Transform Old Google Spreadsheet Code

I used this code to insert documents directly into a Google spreadsheet without having to upload files and then create hyperlinks. However, as google moved to the new spreadsheet...
Warning: The UI service was deprecated on December 11, 2014. To create user interfaces, use the HTML service instead.
So know I need help fixing lines like; var app = UiApp.getActiveApplication();
If someone could clarify the different types of programming direction you could take this project from, that would also be very helpful, I am not really sure if I need to work with Javascript or HTML now.
The answer to your question will depend in large part on how you are using UIApp. In most cases it's not as simple as changing a few lines of code but more of a script redesign.
If you are using it to create a custom user interface, your path forward would be to rebuild your interface as HTML/Javascript and serve it to the user with HTMLService. The service is overall much more powerful/flexible and provides a better end-user experience but it means rewriting how your script works. If you want to take a look at a helpful example on how the HTML Service works take a look at the HTML Service Guide or this quick-start example of an add on.
You can still link this html interface with your script using the google.script.run service but you will need to redesign your code/application to work with the new input methods. The good news is although UI service will sunset on June 30, 2015, "Service will no longer appear in autocomplete, although existing scripts should still function."
It's worth considering alternate ways to accomplish the same goal. The best way to do that is to start by asking "What is the intended use of this script? What problem does it solve?" There may be a better way to accomplish that task which may not even involve updating the script.
This is not related to the new spreadsheet version, the message you get is because UiApp has recently been deprecated.
A Google search would have shown you a few tracks to find your way.
One of the best I found is this one from Sandy Good : Google apps script UI services to HTML services
It shows a typical example of conversion to HTMLService.

Calling jQuery functions from a .gs file (Google Apps Script)

I'm building a community driven website off of Google sites (I know this is a bad choice, I don't have a choice in the matter) and the majority of the functionality is build into Google Apps Scripts (to read/write to an external database and handle how data is displayed). Amid various issues, I've found one that consistently confuses me.
I typically structure my GAS projects into Javascript, HTML, and CSS (Code.gs, HTML.html, and CSS.html). I leverage jQuery for quite a few things (e.g., click handlers, form submission, etc). However, it seems that my jQuery and my Javascript can't exist in the same "world", here meaning in the same .gs file, or the same script tags if it's going in the HTML file. In fact, I can't call any jQuery functions from my .gs file! Can someone explain why this is, please? It seems like the problem is that the jQuery library isn't included, but I can't find a way to include anything in Javascript (think #include from C). And since this is web programming, which is pretty different from "normal" programming, I think that part of my problem understanding this comes from here. And working inside a framework (GAS) doesn't help.
Much obliged, thank you.
HTML Service supports JQuery because the code is executed in your Browser, so the library is loaded in your browser too (after CAJA sanitization) but the code in the .gs file is executed on Google's server and you can't obviously load anything on Google's server !
In one word : there is no way to use JQuery or any other external library in Google Apps Script files other than .html files used in HTML Service.
In the project configuration you're describing, which matches the examples from the HtmlService Best Practices, all your GS code runs server-side, while the regular javascript and jQuery run client-side, in the user's browser. If you've got GS in your HTML files, then I expect you're using templated HTML, which in turn means that the GS is evaluated server-side to produce the final HTML that gets served to the client. (Take a look at the HTML source once the page is served up... no GAS in there.)
So it's those different operating contexts that keep your GAS & jQuery elements from interacting directly. From the client, you can use google.script.run for instance, to call server-side functions and pass parameters to them, and you can also set up call-backs to receive asynchronous "returns" from your GS code.
Given the reliance on DOM elements, I am not sure what use there would be in having jQuery in server-side Apps Script, but this blog entry from #EricKoleda provides an example of porting an open source javascript library to Google Apps Script. (Maybe you'll find his copy of the Underscore Library to be all you need!)

Modifying Google Drive Apps for education platform vs Browser Extension?

I'm working with a few schools to develop a series of interactive textbooks for students. The textbooks need to have randomly generated exercises, for which I've already developed a prototype.
It's also required that the app is managed entirely through Google Drive (eg, I can't use WordPress or even Google Sites), although I can create my own Apps - so long as those can be installed and accessed through Drive.
What I'd like to do is:
Allow authors to create randomly generated worksheets using Google Forms, similar to how they can with my current generator
Allow authors to click on a menu item in Google Docs that embeds a widget/iFrame inside a Doc
Embed JSX Graphs into a Doc
Log students answers into a Spreadsheet
"Pretty-fy" spreadsheet into a nicer display in an instructors page - either in spreadsheet, doc, or custom Google App
Everything must work off of Google Drive
My questions are:
Is this something that can be achieved purely through Google App Scripts?
Would someone viewing the Docs be able to see the embedded widgets without my App Scripts installed (if this is even how it works).
Can I extend Google Apps (when viewed by a visitor) to show a sidebar with ToC
Can cross-document TOC be built, where a TOC is generated from the Headers of all docs in a folder?
Would it be easier to create a Browser Extension that can search for certain strings (or even better, a placeholder image with "please install extension") in the app and replace them with the interactive widgets?
Browser Extension
Aside from extending Google Apps, I could also create a browser extension that students install that does #2...this would be fairly easy. Of course, "Install" is a scary word for parents, so preferably a solution that doesn't require students to install anything.
I guess, really, my direct question is this: Can Google App Scripts alone extend Drive Apps to create interactive textbooks for students and teacher planners.
As commented by Mogsdad, your question is indeed overly broad. But I'll attempt to answer it.
From the list you things you'd like, only point 3 may be problematic. You'd have to test if JSX Graphs works well with Apps Script Html Services. I'm afraid it won't. But you have other Chart solutions on Apps Script.
From your list of questions:
I don't think so, unless you make some compromises on your requirements.
No, only scripts installed on the document/form/spreadsheet itself can embed widgets on it.
Not with Apps Script, to run/interact with scripts the user must be a collaborator, not a visitor (meaning he must have edit rights on the file). Maybe with a browser extension.
Yes. But if there's "too many" files to be processed, you're likely going to hit some Apps Script quotas that will make your life difficult.
I didn't really understood this question. But browser extensions can surely do more than scripts, but they will require installation on each browser for all your users.

Google Form API?

I would like to (programmatically) convert a text file with questions to a Google form. I want to specify the questions and the questiontypes and their options. Example: the questiontype scale should go from 1 to 7 and should have the label 'not important' for 1 and 'very important' for 7.
I was looking into the Google Spreadsheet API but did not see a solution.
(The Google form API at http://code.lancepollard.com/introducing-the-google-form-api is not an answer to this question)
Google released API for this: https://developers.google.com/apps-script/reference/forms/
This service allows scripts to create, access, and modify Google Forms.
Until Google satisfies this feature request (star the feature on Google's site if you want to vote for it), you could try a non-API approach.
iMacros allows you to record, modify and play back macros that control your web browser. My experiments with Google Drive showed that the basic version (without DirectScreen technology) doesn't record macros properly. I tried it with both the plugin for IE (basic and advanced click mode) and Chrome (the latter has limited iMacro support). FYI, I was able to get iMacros IE plug-in to create questions on mentimeter.com, but the macro recorder gets some input fields wrong (which requires hacking of the macro, double-checking the ATTR= of the TAG commands with the 'Inspect element' feature of Chrome, for example).
Assuming that you can get the TAG commands to produce clicks in the right places in Google Drive, the approach is that you basically write (ideally record) a macro, going through the steps you need to create the form as you would using a browser. Then the macro can be edited (you can use variables in iMacros, get the question/questiontype data from a CSV or user-input dialogs, etc.). Looping in iMacros is crude, however. There's no EOF for a CSV (you basically have to know how many lines are in the file and hard-code the loop in your macro).
There's a way to integrate iMacro calls with VB, etc., but I'm not sure if it's possible with the free versions. There's another angle where you generate code (Javascript) from a macro, and then modify it from there.
Of course, all of these things are more fragile than an API approach long-term. Google could change its presentation layer and it will break your macros.
Seems like Apps Script now has a REST API and SDK's for it. Through Apps Script you can generate Google Forms. This API was really hard to find by trying to google for it and I haven't yet tested it myself, but I am going to build something with it today (hopefully). So far everything looks good.
EDIT: Seems like the REST API I am using works very well for fully automated usage.
In March(2022) google released REST API for google form. API allows basic crud operation & also added support for registering watches on the form to notify whenever either form is updated or a new response is received.
As of now (March 2016), Google Forms APIs allow us to create forms and store them in Google Drive. However, Forms APIs do not allow one programmatically modify the form (such as modify content, add or delete questions, pre-filled data, etc). In other words, the form is static. In order to serve custom, external APIs are needed.