I'm trying to write a function that displays paragraph headings from inside a google document onto an HTML sidebar, and allows you to select them (using checkboxes)
After you press a button it will call a function from the Google App Script to format all of the headings in a specific way.
Detailed Process:
- The user presses a refresh button to display all paragraph headings. A Google App Script function (called from HTML file) gets an array of all the headings in the document.
- The array is passed to the HTML file, where a javascript function displays each heading in the array as a checkbox
- The user selects some checkboxes and presses a button. A javascript function gets the # of each checkbox that is selected and for each one, gets the corresponding heading (using array)
- Now, each heading is passed back to the Google App Script, where they are all formatted in a specific way (for example: bolded).
The issue: passing an array of paragraph headings to an HTML file doesn't work since paragraph headings are a feature of google app scripts, so all of the headings return empty when they are passed back to the Google App Script.
Is there a way to get around this?
Things I've Tried:
- Making the array of headings a global variable in GAS (google app
script) and doing everything inside the app script
- Doesn't work: global variables in GAS are static so every time the user refreshes the headings or changes something in the doc, this wouldn't work anymore.
- Using PropertiesService instead of a global function
- Doesn't work: only takes strings as input so I can't input paragraph headings that need to be formatted inside the document :(
Minimal Reproducible Example:
Javascript (in HTML file):
google.script.run.withSuccessHandler(paragraphHeadingTest).getParagraphHeadings();
function paragraphHeadingTest(paragraphHeadings) {
google.script.run.logOutput(paragraphHeadings);
}
Google App Script Code:
function getParagraphHeadings() {
var document = DocumentApp.getActiveDocument().getBody();
var paragraphs = document.getParagraphs();
return paragraphs;
}
function logOutput(output) {
Logger.log(output);
}