google-apps-script - 如何在 Google 表格中复制粘贴带有超链接文本的单元格?
问题描述
我在电子表格 A 中有一个范围,我想将其复制到电子表格 B。原始范围中有一个包含超链接文本的列。
当我尝试将电子表格 A 中的原始范围分配给数组 ( getRange.getValues()
),然后将这些值分配给电子表格 B ( getRange.setValues()
) 中的另一个范围时,我得到了值,但它只是文本,而我期望的是超链接文本。
请帮我从原始范围中获取超链接文本。
解决方案
我相信你的目标如下。
- 您希望使用 Google Apps 脚本将带有超链接的单元格从源 Google 电子表格中的源工作表复制到目标 Google 电子表格中的目标工作表。
为此,这个答案怎么样?在这种情况下,我认为可以通过将 RichTextValues 从源复制到目标来实现您的目标。在这种情况下,getRichTextValues
和setRichTextValues
分别用于代替getValues
和setValues
。
示例脚本:
在使用此脚本之前,请在 Advanced Google services 中启用 Sheets API。
function myFunction() {
// Please set the source Spreadsheet ID, sheet name and range.
const sourceSpreadsheetId = "###";
const sourceSheetName = "Sheet1";
const sourceRange = "A1:B10";
// Please set the destination Spreadsheet ID, sheet name and range.
const destinationSpreadsheetId = "###";
const destinationSheetName = "Sheet1";
const destinationRange = "A1:B10";
const src = SpreadsheetApp.openById(sourceSpreadsheetId).getSheetByName(sourceSheetName).getRange(sourceRange);
const dst = SpreadsheetApp.openById(destinationSpreadsheetId).getSheetByName(destinationSheetName).getRange(destinationRange);
// Modified.
const richTextValues = src.getRichTextValues();
src.getValues().forEach((r, i) => {
r.forEach((c, j) => {
if (typeof c == "number") {
richTextValues[i][j] = SpreadsheetApp.newRichTextValue().setText(c).build();
}
});
});
dst.setRichTextValues(richTextValues);
}
笔记:
- 这是一个简单的示例脚本。所以请根据您的实际情况进行修改。
- 我可以确认你的问题。我可以确认,当通过 检索
getRichTextValues
值时,不包括数字值。我将此报告给问题跟踪器。
参考:
推荐阅读
- android - 为什么我不能使用 Exoplayer?是不是换了其他方法?
- asp.net - AWS SNS 多个订阅者的代码(例如动态实例)
- javascript - 在打字稿定义文件中声明对象属性
- javascript - 使用多个 DropDownList (javascript) 过滤 HTML 表中的数据
- c++ - lambda 可以实例化模板函数吗?
- eclipse - Eclipse 显示奇怪的符号而不是文本
- reactjs - 当 React 组件重新渲染时 UI 不会更新
- c++ - 使用 ParseDelimitedFromCodedInputStream 从文件中读取?
- python - InvalidTextEncoding botocore 异常
- java - 我想关闭 JavaFx 中的启动画面。但它没有关闭,而是第二个窗口正在打开