A Google document contains a dropdown; the values in the dropdown are Sheet names for a given Google Sheet (spreadsheet).
You want to know:
About Google Docs dropdowns
It is worth noting that the options for a dropdown in Google Docs can't be referenced from a range. To the best of my knowledge, they must be entered manually.
Is there a way to allow a script to scan a drop-down on a Google Document?
No.
The only triggers available to Google Docs are:
onOpen
: when a user opens a documentonInstall
: when a user installs an Editor add-ontime-driven
: lets a script execute at a particular time or on a recurring interval.There is no equivalent of onEdit
which is available in Google Sheets. The result is that there is no way to automatically trigger a script based on selecting a value from the dropdown.
Can a Sheet name selected in a Google Docs dropdown be used to run a routine on the relevant Sheets-Sheet
Yes.
Though there is no automatic trigger to run a script in a Google Document, it IS possible to manually run a script that will take a value from a Docs dropdown and run a routine on a given sheet on a given Google spreadsheet.
A method is described in the answer by @qwertzguy in How to get in Apps Script the value of a dropdown in a Google Doc?.
The following script (bound to the Google Doc) demonstrates the method.
function applyDocDropdown() {
var doc = DocumentApp.getActiveDocument()
var id = doc.getId()
var url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=docx&id=" + id
var blob = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob()
blob.setContentType("application/zip")
const content = Utilities.unzip(blob);
let xml = ""
for (let file of content) {
if (file.getName() == "word/document.xml") {
xml = file.getDataAsString()
}
}
var dropDownValues = XmlService.parse(xml)
.getRootElement()
.getDescendants()
.filter(e => e.getType() == "ELEMENT")
.filter(e => e.asElement().getName() == 'dropDownList')
.map(e => ({
type: e.getParentElement().getChildren()[0].getAttributes()[0].getValue(),
currentValue: e.asElement().getAttributes()[0].getValue()
}))
// display all results
Logger.log(dropDownValues) // DEBUG
// display results from dropdown#1
Logger.log(dropDownValues[0]) // DEBUG
// get the dropdown value in dropdown#1
var dropdownSheetName = dropDownValues[0].currentValue
Logger.log("DEBUG: the DOC dropdown sheet name = "+dropdownSheetName)
// get the sheet
var ssId = "<<insert spreadsheet id>>"
var ss = SpreadsheetApp.openById(ssId);
var sheet = ss.getSheetByName(dropdownSheetName)
Logger.log("DEBUG: this is spreadsheet sheet named: "+sheet.getName())
var range = sheet.getRange(1,1)
var value = range.getValue()
Logger.log("DEBUG: the value of cell "+range.getA1Notation()+" = "+value)
}
Script results
[{type=Sheet names, currentValue=Staff}, {currentValue=Option 1, type=my second dropdown}]
{currentValue=Staff, type=Sheet names}
DEBUG: the DOC dropdown sheet name = Staff
DEBUG: this is spreadsheet sheet named: Staff
DEBUG: the value of cell A1 = this is Staff
Note: Cell A1 in each sheet contained a text value = "this is "+sheetname
SAMPLE - Document
SAMPLE - Dropdown: "Sheet names"
SAMPLE - Dropdown: "my second dropdown"