简体   繁体   English

取消链接工作表的所有超链接

[英]Unlink all hyperlinks from a sheet

I'm trying to Unlink all hyperlinks in a Google Spreadsheet, so far I'm trying to do a search and replace which is the code below, but wondering if there is a specific function to do the unlink?我正在尝试取消链接 Google 电子表格中的所有超链接,到目前为止,我正在尝试进行搜索并替换下面的代码,但想知道是否有特定的 function 来执行取消链接?

function replacelinks(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Links");
  var values = sheet.getDataRange().getValues();  
  replaceInSheet(values, '.com', '');
  replaceInSheet(values, '.net', '');

I believe your goal as follows.我相信你的目标如下。

  • You want to remove all hyperlinks on a sheet in a Google Spreadsheet using Google Apps Script.您想使用 Google Apps 脚本删除 Google 电子表格中工作表上的所有超链接。

For this, how about this answer?为此,这个答案怎么样?

Modification points:修改点:

  • Unfortunately, in the current stage, it seems that there are no methods for directly removing the hyperlinks of the cells and texts.不幸的是,在现阶段,似乎没有直接删除单元格和文本的超链接的方法。 So in order to achieve your goal, I would like to propose a workaround.因此,为了实现您的目标,我想提出一个解决方法。 The flow of this workaround is as follows.此解决方法的流程如下。

    1. Copy the sheet you want to remove the hyperlinks in the Google Spreadsheet as a temporal sheet.将要删除 Google 电子表格中的超链接的工作表复制为临时工作表。
    2. Retrieve the richTextValues from the data range of the source sheet.从源工作表的数据范围中检索richTextValues。
    3. Retrieve the cell coordinates, which have the hyperlinks, and create an object for using the method of batchUpdate in Sheets API.检索具有超链接的单元格坐标,并创建一个 object 以使用表格 API 中的 batchUpdate 方法。
    4. Request to the batchUpdate method using the created object.使用创建的 object 请求 batchUpdate 方法。
      • The cells with the hyperlinks on the source sheet are cleared, and the values of the same coordinates are copied from the temporal sheet to the source sheet.源工作表上带有超链接的单元格被清除,相同坐标的值从时态工作表复制到源工作表。 By this, the hyperlinks are removed.这样,超链接就被删除了。

Sample script:示例脚本:

Please copy and paste the following script to the script editor.请将以下脚本复制并粘贴到脚本编辑器中。 And, please enable Sheets API at Advanced Google services .并且, 请在高级 Google 服务中启用表格 API And please set the sheet name you want to remove the hyperlinks.并请设置要删除超链接的工作表名称。

function myFunction() {
  const sheetName = "Sheet1"; // Please set the sheet name.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  const sheetId = sheet.getSheetId();
  const range = sheet.getDataRange();
  const temp = ss.insertSheet();
  const tempId = temp.getSheetId();
  range.copyTo(temp.getRange("A1"), {contentsOnly: true});
  const requests = range.getRichTextValues().reduce((ar, row, i) => {
    row.forEach((col, j) => {
      const runs = col.getRuns();
      if (col.getLinkUrl() || (runs && runs.some(e => e.getLinkUrl()))) {
        const req1 = {updateCells:{range:{sheetId:sheetId,startRowIndex:i,endRowIndex:i + 1,startColumnIndex:j,endColumnIndex:j + 1},fields:"userEnteredValue"}};
        const req2 = {copyPaste:{
          source:{sheetId:tempId,startRowIndex:i,endRowIndex:i + 1,startColumnIndex:j,endColumnIndex:j + 1},
          destination:{sheetId:sheetId,startRowIndex:i,endRowIndex:i + 1,startColumnIndex:j,endColumnIndex:j + 1},
          pasteType:"PASTE_NORMAL"
        }};
        ar = ar.concat(req1, req2);
      }
    });
    return ar;
  }, []);
  Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
  ss.deleteSheet(temp);
}
  • In this script, the hyperlinks of both with and without HYPERLINK() can be removed.在此脚本中,可以删除带有和不带有HYPERLINK()的超链接。

Note:笔记:

  • In the current stage, it seems that getRichTextValues() cannot retrieve the number values.在当前阶段,似乎getRichTextValues()无法检索数字值。 By this, when the number values has the hyperlinks, these hyperlinks cannot be removed.这样,当数值有超链接时,这些超链接不能被删除。 About this issue, I have already reported to the issue tracker.关于这个问题,我已经向问题跟踪器报告了。 Ref So when this issue was removed, I think that using above script, all hyperlinks might be be able to be removed.参考所以当这个问题被删除时,我认为使用上面的脚本,所有的超链接都可以被删除。 Or, in the current stage, the method of getLinkUrl() used in above script is not included in the official document.或者,现阶段,上述脚本中使用的getLinkUrl()方法并未包含在官方文档中。 So I also think that the method for directly removing the hyperlinks might be added in the future update.所以我也认为在以后的更新中可能会增加直接删除超链接的方法。

References:参考:

What helped for me was to setShowHyperlink to false for the range.对我有帮助的是将范围的setShowHyperlink设置为 false。

Example:例子:

const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getActiveSheet();
const range = ws.getRange(lastRow, 1)
range.setValue('https://example.com');
range.setShowHyperlink(false);

But as soon as you double click such a cell, Google will re-apply the hyperlink.但是一旦你双击这样的单元格,谷歌就会重新应用超链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM