├── LICENSE
├── README.md
├── with-HtmlOutput
├── output-client.html
└── output-server.gs
└── with-HtmlTemplate
├── template-client.html
└── template-server.gs
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Zig Mandel
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # htmlService-get-set-data
2 | Send an initialization data object from server .gs to client .html.
3 |
For Google Apps Script [HtmlService](https://developers.google.com/apps-script/guides/html/).
4 |
5 |
This github contains simple, small functions and samples you can copy and paste to your projects.
6 |
7 |
Two different methods: for [HtmlOutput](https://developers.google.com/apps-script/reference/html/html-output) and [HtmlTemplate](https://developers.google.com/apps-script/reference/html/html-template).
8 |
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).
9 |
10 |
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](https://developers.google.com/apps-script/guides/html/reference/run). You can use google.script.run to further communicate with the server (after a user action for example).
11 |
12 | # Licence
13 | Free to use under the MIT Licence https://opensource.org/licenses/MIT
14 |
15 | # Usage
16 | **Method #1, using templates**
17 |
How? Sets a template variable from .gs, then evaluates the template, which inserts the variable inside html javascript.
18 |
Advantages: if data is very large, this might be more efficient as it copies less data to the html output.
19 |
Disadvantages: requires to evaluate the template first (might be more server overhead). This disadvantage is not relevant if you are already using templates.
20 |
Sample: doGetWithTemplates in [template-server.gs](https://github.com/zmandel/htmlService-get-set-data/blob/master/with-HtmlTemplate/template-server.gs) and [template-client.html](https://github.com/zmandel/htmlService-get-set-data/blob/master/with-HtmlTemplate/template-client.html).
21 |
22 |
23 | **Method #2, without templates**
24 |
This method only uses htmlOutput.
25 |
How? Stored in an appended, hidden div. Data is stringified plus encoded in base64 to avoid conflicts with special HTML characters.
26 |
Advantages: does not require templates.
27 |
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
28 |
Sample: doGetWithHtmlOutput in [output-server.gs](https://github.com/zmandel/htmlService-get-set-data/blob/master/with-HtmlOutput/output-server.gs) and [output-client.html](https://github.com/zmandel/htmlService-get-set-data/blob/master/with-HtmlOutput/output-client.html).
29 | * appendDataToHtmlOutput: append a javascript object to an htmlOutput from your .gs file.
30 | * getDataFromHtml: call it from your html file script to get your stored data
31 |
32 | # Working sample script (identical to this repo)
33 | https://script.google.com/d/1bXkZpXnFlw9PUc10ecnb7Scq6ubXfGIrFy8xj33ffen4hw5s-STABaZY/edit?usp=sharing
34 |
35 | # Author
36 | Zig Mandel https://developers.google.com/experts/people/sigmund-zig-mandel
37 |
--------------------------------------------------------------------------------
/with-HtmlOutput/output-client.html:
--------------------------------------------------------------------------------
1 |
2 |
32 |
33 |