如何提取Google智能芯片中的原始URL链接?
提取Google Sheets智能芯片的原始URL解决方案
Google Sheets的常规AppScript方法(如getValues()、getRichTextValues())无法直接获取智能芯片背后的原始URL,因为智能芯片属于特殊的链接实体,元数据未通过基础API暴露。以下是可行的解决方案:
使用Google Sheets高级服务(Advanced Sheets Service)
通过启用Sheets API,我们可以直接读取单元格中智能芯片的链接实体数据,提取对应的URL。
步骤1:启用Sheets高级服务
在AppScript编辑器中:
- 点击菜单栏的 服务 > 添加服务
- 选择 Google Sheets API 并点击 添加
步骤2:脚本示例
function extractSmartChipURLs() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const targetSheet = ss.getActiveSheet(); const targetRange = targetSheet.getDataRange(); // 可替换为指定范围,如getRange("A1:A10") const ssId = ss.getId(); const sheetName = targetSheet.getName(); const startRow = targetRange.getRow(); const endRow = startRow + targetRange.getNumRows() - 1; // 调用Sheets API获取智能芯片的链接实体数据 const apiResponse = Sheets.Spreadsheets.get(ssId, { ranges: [`${sheetName}!${startRow}:${endRow}`], fields: "sheets(data(rowData(values(effectiveValue(linkedEntity)))))", }); // 解析响应并提取URL const rowData = apiResponse.sheets[0].data[0].rowData; rowData.forEach((row, rowIndex) => { if (!row.values) return; row.values.forEach((cell, colIndex) => { if (!cell.effectiveValue?.linkedEntity) return; const entity = cell.effectiveValue.linkedEntity; let targetURL = ""; // 根据智能芯片类型生成对应URL switch(entity.type) { case "DRIVE_FILE": targetURL = `https://docs.google.com/document/d/${entity.driveFile.fileId}/edit`; break; case "DRIVE_FOLDER": targetURL = `https://drive.google.com/drive/folders/${entity.driveFolder.folderId}`; break; case "CONTACT": targetURL = `https://contacts.google.com/u/0/preview/${entity.contact.contactId}`; break; // 可扩展支持其他类型的智能芯片 } // 将URL写入当前单元格的右侧列(可自行调整位置) if (targetURL) { targetSheet.getRange(startRow + rowIndex, colIndex + 2).setValue(targetURL); } }); }); }
原理说明
智能芯片的链接元数据存储在单元格的effectiveValue.linkedEntity字段中,该字段仅能通过Sheets API访问。脚本通过指定fields参数过滤返回数据,仅获取智能芯片的链接实体信息,再根据实体类型拼接对应的原始URL。
内容的提问来源于stack exchange,提问作者Antonio Fergesi
相关产品推荐
相关产品推荐

