Home > Back-end >  Get Google "pills" value as Plain text from Google docs with Google Apps Script
Get Google "pills" value as Plain text from Google docs with Google Apps Script

Time:02-13

I am trying to pull the value of the new feature in Google Docs: the "pills"(unsure about this name).

Google Pills

I am parsing a table in a doc with a Google App Script. When I am getting to the row (see image) I am getting null. I can't figure out what should I run to get this value as Plain text.

The code I run to get this at the moment that returns null:

Logger.log(table.getCell(i, 1).getText())
> null

How can I get the actual value of the "pill"?

CodePudding user response:

I thought that from your provided image, it's the smart chips and it might be Class Date, and also, it seems that it is put into a cell of the column "B". Unfortunately, that cannot be directly retrieved using getText(). I think that this is the reason for your issue.

But, 3 Classes for retrieving the smart chips has been added on August 23, 2021. You can retrieve the value using these Classes. If you want to retrieve the value, how about the following sample script?

Sample script:

When the table of your sample image is used, a simple sample script is as follows.

const cell = table.getCell(i, 1); // This is from your script.

const p = cell.getChild(0).asParagraph();
const c = p.getChild(0);
if (c.getType() == DocumentApp.ElementType.DATE) {
  const value = c.asDate().getDisplayText();
  console.log(value)
} else if (c.getType() == DocumentApp.ElementType.TEXT) {
  const value = c.asText().getText();
  console.log(value)
}

or, if your cells have multiple paragraphs, you can also the following sample script.

const cell = table.getCell(i, 1);  // This is from your script.

for (let j = 0; j < cell.getNumChildren(); j  ) {
  const p = cell.getChild(j).asParagraph();
  for (let k = 0; k < p.getNumChildren(); k  ) {
    const c = p.getChild(k);
    if (c.getType() == DocumentApp.ElementType.DATE) {
      const value = c.asDate().getDisplayText();
      console.log(value)
    } else if (c.getType() == DocumentApp.ElementType.TEXT) {
      const value = c.asText().getText();
      console.log(value)
    }
  }
}
  • When getDisplayText() is changed to getTimestamp(), the Date object can be retrieved instead of the string value.

References:

  • Related