Retrieving Values of Dropdown List of Smart Chips on Google Document using Google Apps Script

Gists

This is a sample script for retrieving the values of dropdown list of the smart chips on Google Document using Google Apps Script.

At August 23, 2021, 3 Classes for retrieving the smart chips have been added to Google Apps Script. But, in the current stage, unfortunately, all values of the smart chips cannot be retrieved by the Classes. For example, the dropdown list of the smart chips cannot be retrieved using the 3 Classes.

When the type of dropdown list of the smart chips is retrieved using the 3 Classes, UNSUPPORTED is returned. And also, when Google Docs API is used, "content": "\n" is returned. So, in the current stage, there are no methods for directly retrieving the value of dropdown list of the smart chips. Of course, I believe that this will be resolved in the future update at the Google side.

In this report, as the current workaround, I would like to proposed a workaround for retrieving the values of the dropdown list of the smart chips.

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.

Sample input situation

This is a sample input situation. You can see a table including the dropdown list of the smart chips in a Google Document.

In this report, the values are retrieved from this table.

Sample script

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.
}

Testing

When this script is tested to the above sample 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' }

Note:

  • Of course, I think that by parsing DOCX data, you can also retrieve the values from the table. But, I thought that when Document service (DocumentApp) can be used, when it is used, the script will be simpler. So, I proposed to convert from DOCX to Document.

Reference

 Share!