首页 > 解决方案 > 如何在 Google 表格中复制粘贴带有超链接文本的单元格?

问题描述

我在电子表格 A 中有一个范围,我想将其复制到电子表格 B。原始范围中有一个包含超链接文本的列。

当我尝试将电子表格 A 中的原始范围分配给数组 ( getRange.getValues()),然后将这些值分配给电子表格 B ( getRange.setValues()) 中的另一个范围时,我得到了值,但它只是文本,而我期望的是超链接文本。

请帮我从原始范围中获取超链接文本。

标签: google-apps-scriptgoogle-sheets

解决方案


我相信你的目标如下。

  • 您希望使用 Google Apps 脚本将带有超链接的单元格从源 Google 电子表格中的源工作表复制到目标 Google 电子表格中的目标工作表。

为此,这个答案怎么样?在这种情况下,我认为可以通过将 RichTextValues 从源复制到目标来实现您的目标。在这种情况下,getRichTextValuessetRichTextValues分别用于代替getValuessetValues

示例脚本:

在使用此脚本之前,请在 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值时,不包括数字值。我将此报告给问题跟踪器

参考:


推荐阅读