Retrieving Values of Calendar Events of Smart Chips on Google Document using Google Apps Script

Gists

This is a workaround for retrieving the values and URLs from the smart chips inserted in Google Document using Google Apps Script.

Recently, the smart chips for Google Document are updated. Ref1 and Ref2 It is considered that this update will advance the collaboration for editing Document. So, there might a case that the information of the smart chips is required to be retrieved. Unfortunately, in the current stage, there are no methods for directly retrieving the information of the smart chips while the smart chips of DATE and PERSON can be retrieved. I believe that all smart chips will be able to be managed with Google Apps Script in the future update.

In this sample script, as a current workaround, I would like to introduce obtaining the event of Google Calendar inserted as the smart chips using Google Apps Script.

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 p = tempDoc.getBody().getParagraphs()[0];
  const value = p.getText();
  const eventLink = p.getLinkUrl();
  console.log(value);
  console.log(eventLink);

  // 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.
}
  • This script uses the following process. Namely, Google Document is converted to DOCX data, and the DOCX data is converted to Google Document. By this flow, the values can be retrieved by Google Apps Script.

  • When this script is run, the following values can be seen in the log.

    sample event 1
    https://www.google.com/calendar/event?eid=###
    
  • About the files inserted as the smart chips can be also retrieved using this workaround.

Reference

 Share!