Reputation: 81
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 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:
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);
}
Upvotes: 2
Views: 226
Reputation: 201513
I understood your issue as follows.
element.editAsText.setUnderline(true);
.getParagraphs()
returns the object of Class Paragraph.
In this modification, getParagraphHeadings()
of Google Apps Script side is modified.
function getParagraphHeadings() {
var document = DocumentApp.getActiveDocument().getBody();
var paragraphs = document.getParagraphs();
return paragraphs;
}
function logOutput(output) {
Logger.log(output);
}
To:
function getParagraphHeadings() {
var document = DocumentApp.getActiveDocument().getBody();
var paragraphs = document.getParagraphs();
return paragraphs.reduce((ar, e, i) => {
if (e.getHeading() == DocumentApp.ParagraphHeading.HEADING1) ar.push({text: e.getText(), index: i});
return ar;
}, []);
}
function logOutput(index) {
var document = DocumentApp.getActiveDocument().getBody();
var paragraph = document.getParagraphs()[index];
paragraph.editAsText().setUnderline(true);
}
google.script.run.withSuccessHandler(paragraphHeadingTest).getParagraphHeadings();
function paragraphHeadingTest(paragraphHeadings) {
google.script.run.logOutput(paragraphHeadings);
}
To:
google.script.run.withSuccessHandler(paragraphHeadingTest).getParagraphHeadings();
function paragraphHeadingTest(paragraphHeadings) {
// do something
const index = 5; // For example, when the index of 5 is selected, the index is sent to Google Apps Script.
google.script.run.logOutput(index);
}
HEADING1
is retrieved as the text type. If you want to other header like HEADING2, HEADING3,,,
, please modify the script.getParagraphHeadings()
returns the object like [{"text":"sample text1","index":1},{"text":"sample text2","index":5},,,]
. When the index of paragraph is sent from HTML side to Google Apps Script, the paragraph can be retrieved using the index.Upvotes: 1