
Google Sheets now supports managing smart chips via its API, enabling retrieval and placement with Apps Script and other languages.
Smart chips in Google Sheets and Docs are a powerful feature designed to enhance collaboration and information sharing. While there historically haven’t been built-in Google Apps Script methods to manage these chips, a workaround was previously developed to address this limitation (Ref).
However, Google has recently introduced direct methods for managing smart chips within the Google Sheets API (Ref). This report provides sample Google Apps Script code that demonstrates how to get and put smart chips on Google Sheets using these new API methods. Although these methods are currently exclusive to Google Sheets, their availability via the Sheets API means they can also be utilized with other programming languages, not just Google Apps Script. It will be anticipated that similar functionality will be extended to Google Docs in future updates.
Please create a new Google Sheets. And, open the script editor.
Please enable the Sheets API at Advanced Google services on the script editor. Ref
Please copy and paste the following script into the script editor.
function insertChips() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheets()[0].setName("Sheet1");// Prepare smart chips.
const stringValue = `Email is @\nSample spreadsheet is @\nSample image file is @`;
const imageFileId = "###"; // Please set your file ID.
const ar = [
{
personProperties: {
email: Session.getActiveUser().getEmail(),
displayFormat: "DEFAULT",
},
},
{ richLinkProperties: { uri: ss.getUrl() } },
{ richLinkProperties: { uri: DriveApp.getFileById(imageFileId).getUrl() } },
];
const chipRuns = [...stringValue.matchAll(/@/g)].map((e, i) => ({
startIndex: e.index,
chip: ar[i],
}));
// Create request body for Sheets API.
const requests = [
{
updateCells: {
rows: [{ values: [{ userEnteredValue: { stringValue }, chipRuns }] }],
range: {
sheetId: sheet.getSheetId(),
startRowIndex: 0,
startColumnIndex: 0,
},
fields: "userEnteredValue,chipRuns",
},
},
];
// Request Sheets API.
Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId());
}
When this script is run, the following result is obtained.
In order to retrieve the data from the smart chips, the following sample script can be used. Please copy and paste the following script to the same script editor and run the function.
function getChips() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const a1Notation = "'Sheet1'!A1";
const obj = Sheets.Spreadsheets.get(ss.getId(), {
ranges: [a1Notation],
fields: "sheets(data(rowData(values(userEnteredValue,chipRuns))))",
});
const res = obj.sheets[0].data[0].rowData.map(({ values }) =>
values.map(({ userEnteredValue: { stringValue }, chipRuns }) => {
return {
text: stringValue,
links: chipRuns.flatMap(
({ chip }) =>
chip?.personProperties?.email || chip?.richLinkProperties?.uri || []
),
};
})
);
console.log(res);
}
When this script is run, the following result is obtained.
[
[
{
"text": "Email is Kanshi Tanaike\nSample spreadsheet is testing smart chips\nSample image file is fig1.jpg",
"links": [
"###@gmail.com",
"https://docs.google.com/spreadsheets/d/###/edit",
"https://drive.google.com/file/d/###/view?usp=drivesdk"
]
}
]
]
Source Credit: https://medium.com/google-cloud/managing-smart-chips-on-google-sheets-with-sheets-api-b6fb5a77ccfe?source=rss—-e52cf94d98af—4