如何用Google Apps Script提取Google Sheets地点智能芯片的名称与链接
提取Google Sheets Place智能芯片的名称与地图链接
可以通过Google Apps Script实现这个需求,Place类型智能芯片本质是带谷歌地图链接的富文本元素,普通的getValues()/getDisplayValues()只会提取显示文本,需要通过富文本相关API获取关联链接。
基础实现代码
适用于单元格仅包含单个Place智能芯片的场景:
function extractPlaceChipData() { // 替换为你的工作表名称和目标列范围 const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); const targetRange = sheet.getRange("A2:A" + sheet.getLastRow()); const richTextValues = targetRange.getRichTextValues(); const placeResults = []; // 遍历每个单元格提取数据 richTextValues.forEach((row, index) => { const cellRichText = row[0]; const mapUrl = cellRichText.getLinkUrl(); // 仅处理包含有效谷歌地图链接的单元格 if (mapUrl && mapUrl.includes("google.com/maps")) { placeResults.push({ rowNumber: index + 2, // 对应原表格行号(从第二行开始) placeName: cellRichText.getText(), googleMapUrl: mapUrl }); } }); // 打印结果或写入表格 console.log("提取到的Place芯片数据:", placeResults); // 示例:将结果写入B、C列 placeResults.forEach((data, idx) => { sheet.getRange(data.rowNumber, 2).setValue(data.placeName); sheet.getRange(data.rowNumber, 3).setValue(data.googleMapUrl); }); }
进阶处理:单元格含多个富文本片段
如果单元格内除了Place芯片还有其他文本,需要遍历富文本片段筛选出地图链接:
function extractMultiPlaceChips() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); const targetRange = sheet.getRange("A2:A" + sheet.getLastRow()); const richTextValues = targetRange.getRichTextValues(); const placeResults = []; richTextValues.forEach((row, rowIndex) => { const cellRichText = row[0]; const textRuns = cellRichText.getRuns(); // 获取单元格内所有富文本片段 textRuns.forEach((run, runIndex) => { const mapUrl = run.getLinkUrl(); if (mapUrl && mapUrl.includes("google.com/maps")) { placeResults.push({ rowNumber: rowIndex + 2, segmentIndex: runIndex + 1, placeName: run.getText(), googleMapUrl: mapUrl }); } }); }); console.log(placeResults); }
关键说明
- Place智能芯片的链接会直接指向该地点的谷歌地图页面,通过
getLinkUrl()即可获取完整URL - 需注意判断链接是否包含
google.com/maps,避免误提取其他类型的富文本链接 - 执行脚本前需确保已获得Google Sheets的授权权限
内容的提问来源于stack exchange,提问作者FreeCoder
相关产品推荐
相关产品推荐

