Send an initialization data object from server .gs to client .html.
For Google Apps Script HtmlService.
This github contains simple, small functions and samples you can copy and paste to your projects.
Two different methods: for HtmlOutput and HtmlTemplate.
Both methods safely store any (serializable) javascript object taking care of possible issues regarding security (code injection) or formatting (conflicts with html special characters in the data).
Both methods are more efficient than having the client call the server to get the data object after your initial Javasctipt+html skeleton loads in the client, which requires an aditional client-side call. See google.script.run. You can use google.script.run to further communicate with the server (after a user action for example).
Free to use under the MIT Licence https://opensource.org/licenses/MIT
Method #1, using templates
How? Sets a template variable from .gs, then evaluates the template, which inserts the variable inside html javascript.
Advantages: if data is very large, this might be more efficient as it copies less data to the html output.
Disadvantages: requires to evaluate the template first (might be more server overhead). This disadvantage is not relevant if you are already using templates.
Sample: doGetWithTemplates in template-server.gs and template-client.html.
Method #2, without templates
This method only uses htmlOutput.
How? Stored in an appended, hidden div. Data is stringified plus encoded in base64 to avoid conflicts with special HTML characters.
Advantages: does not require templates.
Disadvantages: If the data is very large, it will increase the HTML file size (about 33% overhead of data size in html compared with method #1). If that is a concern (when passing huge objects) you may use base94 or even base128 encoding but requires more code and can have issues, see http://stackoverflow.com/questions/6008047/why-dont-people-use-base128
Sample: doGetWithHtmlOutput in output-server.gs and output-client.html.
- appendDataToHtmlOutput: append a javascript object to an htmlOutput from your .gs file.
- getDataFromHtml: call it from your html file script to get your stored data
Zig Mandel https://developers.google.com/experts/people/sigmund-zig-mandel