Apps脚本:如何从没有公式的单元格中获取超链接

Irf*_*lam 7 cell hyperlink google-sheets google-apps-script google-sheets-formula

我有一个工作表,其中在单元格中设置了超链接,但没有通过公式设置。单击单元格时,在“ fx”栏中,它仅显示该值。

我在网上搜索,但到处都是,信息是通过使用提取超链接getFormula()

但就我而言,根本没有设定公式。

I can see hyperlink as you can see in image, but it's not there in "formula/fx" bar.

在此处输入图片说明

How to get hyperlink of that cell using Apps Script or any formula?

小智 12

当单元格只有一个 URL 时,您可以使用以下简单脚本从单元格中检索 URL。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var url = sheet.getRange()("A2").getRichTextValue().getLinkUrl();
Run Code Online (Sandbox Code Playgroud)

来源:https : //gist.github.com/tanaikech/d39b4b5ccc5a1d50f5b8b75febd807a6


Tan*_*ike 5

When Excel file including the cells with the hyperlinks is converted to Google Spreadsheet, such situation can be also seen. In my case, I retrieve the URLs using Sheets API. A sample script is as follows. I think that there might be several solutions. So please think of this as one of them.

When you use this script, please enable Sheets API at Advanced Google Services and API console. You can see about how to enable Sheets API at here.

Sample script:

var spreadsheetId = "### spreadsheetId ###";
var res = Sheets.Spreadsheets.get(spreadsheetId, {ranges: "Sheet1!A1:A10", fields: "sheets/data/rowData/values/hyperlink"});
var sheets = res.sheets;
for (var i = 0; i < sheets.length; i++) {
  var data = sheets[i].data;
  for (var j = 0; j < data.length; j++) {
    var rowData = data[j].rowData;
    for (var k = 0; k < rowData.length; k++) {
      var values = rowData[k].values;
      for (var l = 0; l < values.length; l++) {
        Logger.log(values[l].hyperlink) // You can see the URL here.
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Note:

  • Please set spreadsheetId.
  • Sheet1!A1:A10 is a sample. Please set the range for your situation.
  • In this case, each element of rowData is corresponding to the index of row. Each element of values is corresponding to the index of column.

References:

If this was not what you want, please tell me. I would like to modify it.

  • 这就是我想要的。但它不应该是 Advance API 的一部分。电子表格的类范围中应该有一些方法可以轻松实现这一点。不管怎样,谢谢! (2认同)