I'm a beginner working with Google Apps Script to pull data from a Google Doc, and I need some help...
I have a Google Doc that has a ton of cooking recipes. I'd like to write a function that randomly selects 4 recipes, and emails me the ingredients so I know what to shop for that week. All my recipes titles are 'Heading 3', with the ingredients as bullet points below them. I'm fully open to modifying the formatting if need be.
I think I have a way to identify all text that is of type 'Heading 3', but I need a way to store them in an array as text, to then randomly select 4 of them. I can't seem to solve this...
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Generate Weekly Shopping List')
.addItem('Send Email', 'generateMenu')
.addToUi();
}
function generateMenu() {
var ps = DocumentApp.getActiveDocument().getBody()
var searchType = DocumentApp.ElementType.PARAGRAPH;
var searchHeading = DocumentApp.ParagraphHeading.HEADING3;
var searchResult = null;
while (searchResult = ps.findElement(searchType, searchResult)) {
var par = searchResult.getElement().asParagraph();
if (par.getHeading() == searchHeading) {
// Found one, update Logger.log and stop.
var h = searchResult.getElement().asText().getText();
return h;
//how do I store this back into an array...then randomly select 4?
}
// Get the email address of the active user - that's you.
var email = Session.getActiveUser().getEmail();
// Send yourself an email with a link to the document.
GmailApp.sendEmail(email, "Shopping List For The Week", "Here is the shopping list:" + h);
}
}