Copying Google Doc text into a google sheet based on the Heading - google-apps-script

I am a project manager for a construction company. I have a Google doc with a Master To-Do list for different Jobs and subcontractors. https://docs.google.com/document/d/14oVky5DA3xtjE7COMVAx6l26WXb35HvsL8V9dng3eIc/edit?usp=sharing
I want to be able to use a menu item to update a google sheet with items for a specific subcontractor.
So far I can make it find the heading. I'm Not sure how to go about "digging" down into the specific items below it.
I should be able to put it into the appropriate spreadsheet from there. I'm still super new to coding so let me know if I need to clarify anything.
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Update Subs')
.addItem('Update', 'get_some_heading') //done
.addToUi();
}
function get_some_heading() {
var GlobalText = DocumentApp.getActiveDocument().getBody()
var searchType = DocumentApp.ElementType.PARAGRAPH;
var searchSubNames = DocumentApp.ParagraphHeading.HEADING4;
var searchResult = null;
while (searchResult = GlobalText.findElement(searchType, searchResult)) {
var par = searchResult.getElement().asParagraph();
var SubName = searchResult.getElement().asParagraph().asText().getText();
if (par.getHeading() == searchSubNames) {
if (SubName == "Subcontractor 2"){
DocumentApp.getUi().alert(SubName);
return SubName;
}
}
}
}

Related

Copy Document Title Using Google App Script

I want to copy the text from the title within this document, but I am only used to using Google App Script in sheets, not docs:
I have tried lot of different methods, but nothing is populating in Logger.log(), so I am not sure it is working. Currently, all I want to return is the text in the Title, so I can go on to search for this in a spreadsheet. So far I have the following:
function autoFill() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var searchResult = body.findElement(DocumentApp.ElementType.PARAGRAPH);
var searchHeading = searchResult.getElement(DocumentApp.ParagraphHeading.TITLE);
The only part of the document that has the format of Title is the first line in the document.
Here is an example of how to find the paragraph with the Heading style Title. In my Doc I have a line of text "This is some text" with style Title.
function findTitle() {
try {
let doc = DocumentApp.getActiveDocument();
let body = doc.getBody();
let paras = body.getParagraphs();
let text = null;
paras.some( para => {
let attribs = para.getAttributes();
if( attribs["HEADING"] === DocumentApp.ParagraphHeading.TITLE ) {
text = para.getText();
return true;
}
return false;
}
);
console.log(text);
}
catch(err) {
console.log(err);
}
}
Execution log
8:08:39 AM Notice Execution started
8:08:39 AM Info This is some text
8:08:39 AM Notice Execution completed
Found that using Regular Expression would be able to identify the text needed:
function autoFill() {
var ss = DocumentApp.getActiveDocument();
var body = ss.getBody();
var header = DocumentApp.getActiveDocument().getHeader();
var contents = body.getText();
var racm = SpreadsheetApp.openById('[enter sheet ID').getSheetByName('Risks & Controls matrix');
var lr = racm.getLastRow();
var colID = racm.getRange(1,1,lr,1).getValues();
//you need to tweak below to the control name pattern
var regexName = /\w+[-]\w+[-]\w+/ //use website: regexr.com to create & www.geeksforgeeks.org/write-regular-expressions/
var titleFound=regexName.exec(contents)[0];
let row = colID.findIndex(users => {return users[0] == titleFound})+1;
Then goes on to finding the relevant data in the ranges from the spreadsheet and .replaceText() within the doc.
Works really well. Thanks for your help!

Google Script problem while updating events to Google Calendar (trying not to make duplicates)

Kinda new to Google Apps Script. I'm trying to create Google Calendar events from GoogleSheet. It won't be revealing that I'm using someone's answers where some answers are here from stackoverflow. Unfortunately, I have not found a complete answer to my problem. Or if there was I couldn't recreate it :)
GoogleSheet
That's the Google Sheet that I'm using (trying to make a shipping calendar where someone can track deliveries). Columns marked as 0 are not used to make calendar events, only these marked as 1. For example what should be in Calendar Event:
Title: L-H-19/22/Description: Driver 1 Mobile: 1234567890/Date:2022-05-16
Note: Date probably will be changed to start/end date with hours. (2022-05-16 06:00 - 2022-5-16 07:00). Also, I mentioned "Description" which I'm not using but I'll give it a go, while I change ".createAllDayEvent" to ".createEvent", that's why also there is commented "DATA_2".
For now, it creates an event but I cannot figure out how to update it without making duplicates.. Tried to use .deleteEventSeries() and update it, also tried to use trigger onEdit() but still without luck.
Here is code which I use to add events to calendar:
function initMenu() {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('SPEDYCJA')
menu.addItem('DODAJ DO KALENDARZA','DodanieSpedycjiDoKalendarza')
menu.addItem('AKTUALIZUJ KALENDARZ','AktualizacjaWydarzenia')
menu.addToUi();
}
function onOpen() {
initMenu();
}
function DodanieSpedycjiDoKalendarza() {
var ui = SpreadsheetApp.getUi();
var WcisnietyPrzycisk = ui.alert("Czy na pewno chcesz uruchomić skrypt??",ui.ButtonSet.YES_NO);
if (WcisnietyPrzycisk == ui.Button.YES) {
let AktywnyArkusz = SpreadsheetApp.getActiveSheet();
let KalendarzSpedycja = CalendarApp.getCalendarById("cbvdt6iek0qbgujgbhq68et950#group.calendar.google.com");
let TabelaDanych = AktywnyArkusz.getRange(12,8,8,9).getValues();
for (let x=0; x<TabelaDanych.length; x++) {
const ZMIANA = TabelaDanych[x];
const TYTUŁ = ZMIANA[0];
const DATA_1 = ZMIANA[7];
//const DATA_2 = ZMIANA[14];
const ID_Wydarzenia = ZMIANA[8];
if (ID_Wydarzenia == "") {
const NoweWydarzenie = KalendarzSpedycja.createAllDayEvent(TYTUŁ,DATA_1);
const ID_NoweWydarzenie = NoweWydarzenie.getId();
AktywnyArkusz.getRange(8+x,8).setValue(ID_NoweWydarzenie);
}
try {
var event = KalendarzSpedycja.getEventSeriesById(ID_Wydarzenia);
event.deleteEventSeries();
//entry[9] = '';
} catch(e) {
//nie rób nic
}
var newEvent = KalendarzSpedycja.createAllDayEvent(TYTUŁ,DATA_1);
// entry[9] = newEvent;
debugger;
}
ui.alert("Dodano spedycje do kalendarza!")
} else if (WcisnietyPrzycisk == ui.Button.NO) {
ui.alert("Nie uruchomiłeś skryptu.");
}
}
Also, sample code where I tried to use trigger onEdit() while then in the main function I did not used "try { .deleteEventSeries() }", there was only loop for but still failed..
function AktualizacjaWydarzenia(e) {
var ZaktualizowaneWiersze = e.range.getRange();
var ZaktualizowaneDane = e.source.getActiveSheet().getRange(ZaktualizowaneWiersze, 8, 8, 9).getValues()[4];
var ID_ZaktualizowaneWydarzenie = ZaktualizowaneDane[9]
try {
var Wydarzenie = CalendarApp.getEventById(ID_ZaktualizowaneWydarzenie);
ID_ZaktualizowaneWydarzenie.setTitle(ZaktualizowaneDane[1]);
ID_ZaktualizowaneWydarzenie.setDate(ZaktualizowaneDane[8]);
} catch(err) {
console.log("Wystąpił błąd podczas aktualizowania wydarzeń do kalendarza. Konkretne wydarzenie może jeszcze nie istnieć.");
}
}
I will be very grateful where someone will paste answer with code and point me/critique in existing one what I did wrong.
Thank you, have a nice day.
To prevent duplicates,
you need to save in one column the event id when you create that event. Then, if you want to update, you have to take account of this id.
reference
setTime(startTime, endTime)
try
function AktualizacjaWydarzenia(ID_Wydarzenia,TYTUŁ,DATA_1,DATA_2) {
var Wydarzenie = CalendarApp.getEventById(ID_ZaktualizowaneWydarzenie);
Wydarzenie.getEventById(ID_Wydarzenia).setTitle(TYTUŁ)
Wydarzenie.getEventById(ID_Wydarzenia).setTime(DATA_1,DATA_2)
}

Storing an array of values from Docs in Google Apps Scripts

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);
}
}
The first function generates an array of objects from your document
function generateObj() {
var body = DocumentApp.getActiveDocument().getBody()
var children=body.getNumChildren();
//var html='';
var rObj={rA:[]}
for(var i=0;i<children;i++) {
var child=body.getChild(i);
if(child.getType()==DocumentApp.ElementType.PARAGRAPH && child.asParagraph().getHeading()==DocumentApp.ParagraphHeading.HEADING3 && child.asParagraph().getText().length>0) {
//html+='<br />' + child.asParagraph().getText();
var prop=child.asParagraph().getText();
rObj.rA.push(prop);
rObj[prop]=[];
var n=1;
while(body.getChild(i+n).getType()==DocumentApp.ElementType.LIST_ITEM) {
//html+='<br />'+body.getChild(i+n).asListItem().getText();
rObj[prop].push(body.getChild(i+n).asListItem().getText());
n++;
}
i+=n-1;
}
}
//DocumentApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'Results')
return rObj;
}
//defaults to 4 selections
function pikn(n=4) {
var rObj=generateObj();//get array of objects
var rA=[];
var t=rObj.rA.slice();//copy array to temp array
for(var i=0;i<n;i++) {
var idx=Math.floor(Math.random()*t.length);//pick random index
rA.push(t[idx]);//save title of recipe
t.splice(idx,1);//remove that index
}
var s='';
//loop through selected recipes which are also object properties
rA.forEach(function(r,i){
var items=rObj[r];
s+='\n' + r;
items.forEach(function(item,j){s+='\n' + item});//loop through recipe items and collect everything in s as simple text to insert into standard body
});
GmailApp.sendEmail("Your email address","Random Recipes",s);
}
Requires Chrome V8

How to do drop-down data validation in a prompt dialogue using google script?

I'm trying to use google script to create a prompt dialogue for the user with a drop-down list.
Here's where I'm at so far:
function addProject() {
var ui = SpreadsheetApp.getUi(); // Same variations.
var result = ui.prompt(
'Select project from drop-down',
'(use "Project Index" sheet to add a project)',
ui.ButtonSet.OK);
// Process the user's response.
var button = result.getSelectedButton();
var text = result.getResponseText();
if (button == ui.Button.OK) {
// User clicked "OK".
ui.alert('You selected ' + text + '.');
} else if (button == ui.Button.CANCEL) {
// User clicked "Cancel".
ui.alert('I didn\'t get your project.');
}
}
A button in my sheet activates this function. All this does is create a prompt that you can enter a string into. But I need to use data validation to create a drop-down list that references a range elsewhere in my sheet so that the user can enter anything they want.
Can someone help me get a drop-down list in a prompt dialogue?
Thanks!
Unfortunately you can not use ui.prompt in this way. Your best bet is to use a Custom Dialog in which you can put a dropdown.
edit: For a partial code answer, check out the answer here, How to create a drop down list in (App Script) Spreadsheet Input Box?
function NewItem() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('My Application');
var panel = app.createVerticalPanel();
var lb = app.createListBox(true).setId('myId').setName('myLbName');
var sh = SpreadsheetApp.getUi();
var response = sh.alert("Confirm!", "This will delete the selected item in all sheets, do you want to continue?", sh.ButtonSet.OK_CANCEL)
if (response == sh.Button.OK)
{
// List of categories for user to select
lb.setVisibleItemCount(9);
lb.addItem('Category 1');
lb.addItem('Category 2');
lb.addItem('Category 3');
lb.addItem('Category 4');
panel.add(lb);
var button = app.createButton('OK');
var handler = app.createServerClickHandler('click').addCallbackElement(panel);
button.addClickHandler(handler);
panel.add(button);
app.add(panel);
doc.show(app);
}
if (response == sh.Button.CANCEL)
{
sh.alert("Canceled", "The action is canceled", sh.ButtonSet.OK)
}
}
function click(eventInfo) {
var app = UiApp.getActiveApplication();
// get category that user chose
var value = eventInfo.parameter.myLbName;
var doc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("items_with_category");
var RevenueItem = Browser.inputBox("Copy paste the name of the revenue item from the QB report:");
var COSItem = Browser.inputBox("Copy paste the name of the COS item from the QB report:");
var Client = Browser.inputBox("Copy past the client from QB report:");
var RevAcct = Browser.inputBox("Revenue Account Code (eg. 4001):");
var COSAcct = Browser.inputBox("Revenue Account Code (eg. 5001):");
var lastRow = doc.getLastRow()+1;
//doc.getRange(lastRow,1).setValue(value);
doc.getRange(lastRow,1).setValue(COSItem);
doc.getRange(lastRow,2).setValue(RevenueItem);
doc.getRange(lastRow,3).setValue(Client);
doc.getRange(lastRow,4).setValue(value);
}

Uploading Google Spreadsheet to a Google Site

I'd like to begin by stating that the end goal is to display our company directory (a list of our employees names/job title/extension#/office location/email), which is in a Google Sheet, on a page in one of our Google Sites.
I tried to use Google's embed function, and it works... but it is very clunky, does not have a "Sort" function, and it just looks weird.
I pulled a Google Apps Script from somewhere online like 3 months ago and it actually did pull in a way that made me happy:
(This is as it appears currently on the Google Sites page. So in this screenshot, the embedded Sheet is at the top. The Sheet when, imported via the script, is below. Yes, they are both on the same page. I'm in testing!)
This is the code I used (I THINK - I don't remember how I implemented it):
function myFunction() {
}
function onOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
if(ScriptProperties.getProperty("page url") == null){
ss.addMenu("List page", [{name: "Create list", functionName: "create_list"},null,
{name: "Fetch list items", functionName: "fetch_items"}]);
}
else{
ss.addMenu("List page", [{name: "Push Items", functionName: "push_items"}]);
}
}
function create_list() {
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var parent_page = Browser.inputBox("URL of the parent page:");
var title = Browser.inputBox("Choose a name for your list page:");
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var list = SitesApp.getPageByUrl(parent_page).createListPage(title, title.split(' ').join(''), '', data[0]);
ScriptProperties.setProperty("page url", list.getUrl());
onOpen();
push_items();
}
function push_items(){
var done = false;
while(!done){
try{
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var list = SitesApp.getPageByUrl(ScriptProperties.getProperty("page url"));
var list_items = list.getListItems();
for(i in list_items){
list_items[i].deleteListItem();
}
for(var i = 1; i < data.length; i++){
var item = list.addListItem(data[i]);
}
done = true;
}
catch(e){
}
}
SpreadsheetApp.getActiveSpreadsheet().toast(ScriptProperties.getProperty("page url"), "List page updated", 10);
}
function fetch_items(){
var url = Browser.inputBox("URL of your list page:");
var col_number = Browser.inputBox("Number of columns in the list:");
var data = new Array();
var list_items = SitesApp.getPageByUrl(url).getListItems();
for(i in list_items){
var row = new Array();
for(j = 0; j < col_number; j++){
row.push(list_items[i].getValueByIndex(j));
}
data.push(row);
}
SpreadsheetApp.getActiveSheet().getRange(1, 1, data.length, data[0].length).setValues(data);
}
[I do not take credit for writing this!]
So I would like to ask (since this ceases to make much sense to me) is if this is viable code for a Google Apps Script, and if so, how do I implement it to output Sheet data similarly in the same type of format as in the screenshot?
Alternatively, is there a better way to display this Sheet data in Google Sheets?
A totally different alternative would be to use Romain Vialard's "awesome tables" gadget. It works... awesome, and it is really easy to use. Besides, it admits filters, ...