I am working on a way auto-populate my google docs using google apps scripting. I want to populate a dropdown in a sidebar with a column from my google sheet, then fetch this on the server side when it changes.
function onOpen() {
DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('FCAR')
.addItem('Select Class', 'showSidebar')
.addToUi();
}
function showSidebar() {
var sheet = SpreadsheetApp.openById("XXXXXXXXXXXXX").getSheetByName("Sheet1");
var data = sheet.getSheetValues(1, 1, sheet.getLastRow(), sheet.getLastColumn());
console.log(sheet.getLastColumn());
var classes = [];
for(var i = 0; i < sheet.getLastColumn()-1; i++) {
classes.push(data[i][0]);
}
console.log(classes);
var html = HtmlService.createHtmlOutput();
html.append(generateHtml(classes));
console.log(html.getContent())
DocumentApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
// Function to generate HTML string for the dropdown
function generateHtml(options) {
var dropdownHTML = '<base target="_top">';
dropdownHTML += '<select id="myDropdown" onchange="dropdownChanged(this)">';
options.forEach(function(option) {
dropdownHTML += '<option value="' + option + '">' + option + '</option>';
});
dropdownHTML += '</select>';
// Add client-side JavaScript function
dropdownHTML += '<script>';
dropdownHTML += 'function dropdownChanged(dropdown) {';
dropdownHTML += 'var selectedValue = dropdown.value;';
dropdownHTML += 'alert("Selected value: " + selectedValue);';
dropdownHTML += 'google.script.run.doSomething(selectedValue);'; // Call server-side function
dropdownHTML += '}';
dropdownHTML += '</script>';
return dropdownHTML;
}
// Function to handle dropdown change event
function doSomething(value) {
// Get the active Google Document
var doc = DocumentApp.getActiveDocument();
// Get the header of the document
var header = doc.getHeader();
// Set the text of the header to the selected value
header.appendParagraph(value);
}
The drop down is populated successfully, and when I change it, I get the alert of the new value, but doSomething does not execute or give any error.
Any help is appreciated, Thanks!
About
The drop down is populated successfully, and when I change it, I get the alert of the new value, but doSomething does not execute or give any error., it seems thatdoSomethingis not used in your script. And atgoogle.script.run.dropdownChanged(selectedValue);, the functiondropdownChangedis tried to be run in the Google Apps Script side. I guess that this might be the reason for your current issue. In this case, how about modifying it todoSomething? When this is reflected in your script, it becomes as follows.From:
To:
By this, the function
doSomethingat the Google Apps Script is run with a value ofselectedValue.Note:
doSomethingmight not be able to put the value to the 1st page header. Please be careful about this.Added 1:
From your following reply,
Although I'm not sure about your actual Google Document, I'm worried about "Note" section in my answer. If you are required to enable the 1st page header, please modify your function
doSomethingas follows. By this,appendParagraph(value)is run on both the 1st page header and other pages.Added 2:
From your following reply,
In this case, please modify
doSomethingas follows.In the case of new Document, no header is existing as the default. So, it is required to use
addHeader.Added 3:
From your following reply,
I think that you can see the error message in "Executions" of the script editor. But, I'm worried that you might not be able to understand this. So, please modify the function
doSomethingas follows. By this, when an error occurs atdoSomething, the error message is shown in the dialog.