Google Docs now supports dropdown inputs (Insert > Dropdown
). I am using Apps Script to access a Google Docs, and would like to get the value of a drop down.
I have read Get Google "pills" value as Plain text from Google docs with Google Apps Script, but unfortunately this solution doesn't work.
However, when getting the element in my Apps Script, it looks like the element is unsupported.
const statusCell = table.getRow(1).getCell(1);
const p = statusCell.getChild(0).asParagraph();
const c = p.getChild(0);
console.log("---> " + c);
console.log("tpe: " + c.getType());
// ---> UnsupportedElement
// tpe: UNSUPPORTED
If I replace the content of the cell with plain text, then everything works. I'm only having issues with drop downs.
Is it possible to get the value of a drop down in Google Docs from Apps Script?
I believe your goal is as follows.
Unfortunately, in the current stage, it seems that there are no built-in methods of Google Apps Script for directly retrieving the values of the dropdown list of the smart chips. When getType()
is used, UNSUPPORTED
is returned as you have already mentioned in the comment. And also, even when Google Docs API is used, "content": "\n",
is returned. In this case, I would like to recommend reporting this to the Google issue tracker as a future request.
From the above situation, I would like to propose a workaround. In this workaround, the Google Document is converted to DOCX data. And, the DOCX data is converted to Google Document. By this conversion, the texts of the smart chips can be retrieved. When this flow is reflected in a script, it becomes as follows.
Please copy and paste the following script to the script editor of Google Document. And, please enable Drive API at Advanced Google services.
function myFunction() {
const doc = DocumentApp.getActiveDocument();
const id = doc.getId();
const url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=docx&id=" + id;
const blob = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob();
const tempFileId = Drive.Files.insert({ title: "temp", mimeType: MimeType.GOOGLE_DOCS }, blob).id;
const tempDoc = DocumentApp.openById(tempFileId);
const table = tempDoc.getBody().getTables()[0];
for (let r = 0; r < table.getNumRows(); r++) {
const row = table.getRow(r);
for (let c = 0; c < row.getNumCells(); c++) {
const cell = row.getCell(c);
console.log({ row: r, col: c, text: cell.getText() });
}
}
// DriveApp.getFileById(tempFileId).setTrashed(true); // If you want to delete the tempolary document, please use this.
// DriveApp.createFile(); // This is used for automatically detecting the scope by the script editor.
}
When this script is tested to the above Document, the following result is obtained.
{ row: 0, col: 0, text: 'sample1' }
{ row: 0, col: 1, text: 'sample2' }
{ row: 0, col: 2, text: 'sample3' }
{ row: 1, col: 0, text: 'sample3' }
{ row: 1, col: 1, text: 'sample2' }
{ row: 1, col: 2, text: 'sample1' }