Google 表格脚本,用于将超链接引用的文本从一张纸复制到另一张纸中

问题描述 投票:0回答:1

我在工作表中有一行单元格,其中一些包含超链接。这些超链接引用同一工作表中包含文本的单元格/单元格范围。我想运行一个脚本,该脚本查看包含超链接的单元格范围,从超链接引用的单元格中提取文本,并将该文本粘贴到新工作表中的一列单元格中。经过多次失败的尝试后,Chat GPT 提供了以下代码:

function copyLinkedText() {
    var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Marcus Nature Calendar Gannt Master");
    var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Hyperlinked Flag Text");

    var rangeWithHyperlinks = sourceSheet.getRange("O8:NP8");
    var values = rangeWithHyperlinks.getValues();

    for (var i = 0; i < values.length; i++) {
        for (var j = 0; j < values[0].length; j++) {
            var formula = values[i][j];
            if (formula !== '') {
                var parts = formula.match(/'([^']+)'/);
                if (parts && parts.length >= 2) {
                    var linkedSheetName = parts[1];
                    var linkedCell = parts[2];
                    var linkedText = sourceSheet.getRange(linkedSheetName + '!' + linkedCell).getValue();
                    targetSheet.getRange(i + 1, j + 1).setValue(linkedText);
                }
            } else {
                targetSheet.getRange(i + 1, j + 1).setValue('');
            }
        }
    }
}

代码执行没有错误,但没有从源工作表中提取数据并将其传输到目标工作表。我怀疑这是用于超链接的格式的问题(?) - 它们不是公式格式,但看起来像这样,例如。 '工作表名称'!A16:A24。这就是当我将鼠标悬停在包含超链接的单元格上时看到的内容。

任何人都可以帮助我实现我想要做的事情吗?提前致谢。顺便说一句,我是一个脚本新手,因此我依赖 Chat GPT。

我尝试了上面的代码,运行时没有错误,但什么也没做。

google-sheets hyperlink scripting text-extraction
1个回答
0
投票

建议:使用
getRichTextValues()

Chat GPT 推荐的代码不适用于您的情况,因为链接不是通过公式插入的。如果我错了,请纠正我,但我假设您已经使用 Get link to this cell 选项来获取单元格的超链接。

如果确实如此,通过

getRichTextValues()
获取超链接应该可以工作,这也应该允许您访问引用的单元格并复制值。

请尝试这个脚本:

function copyLinkedText() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName('Source_sheet'); //Replace this with the name of the source sheet.
  var destinationSheet = ss.getSheetByName('Destination_sheet'); //Replace this with the name of the target sheet.
  var sourceValues = sourceSheet.getRange('O8:NP8').getRichTextValues().flat(); //Gets the rich text value of the range. Adjust the range if needed.
  var linkValues = [] // Array to store the texts inside the cells referenced by the links.

  sourceValues.forEach(x => {
    //Loops through each cell on the row and gets the link URL
    var url = x.getLinkUrl();
    if (url != null) {
      var linkRange = url.slice(url.indexOf('range=')).replace('range=', ''); // If there's a URL, extracts the location of the cell it references.
      linkValues.push([sourceSheet.getRange(linkRange).getValue()]); //Gets the value of the referenced cell and adds it to the Array.
    }
  })
  /*
  The last part of the code below inserts the texts to the target sheet.
  Do note that you would have to adjust the range if needed. 
  With how it is configured, it will insert the text on Column A, starting from A1
  */
  destinationSheet.getRange(1, 1, linkValues.length).setValues(linkValues);
}

如果需要,请不要忘记更换

soureSheet
destinationSheet
的范围。

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.