问题
I have asked long time ago and got a great answer to the this question: "Google Sheets: count numbers of HYPERLINKS where value > 0" Google Sheets: count numbers of HYPERLINKS where value > 0
Now google has changed their hyperlinks to insert multiple hyperlinks in a cell - nice... But they also do NOT insert it in this way anymore:
=HYPERLINK("http:/www.test.com", 100")
So now my old question comes up again: How to count number of cells with HYPERLINKS where value > 0
回答1:
I believe your goal as follows.
- You want to retrieve the number of hyperlinks from a range on a sheet in the Google Spreadsheet.
You want to achieve this by modifying the following Google Apps Script. Ref
function countLinks(rangeNotation) { var sheet = SpreadsheetApp.getActiveSheet(); var formulas = sheet.getRange(rangeNotation).getFormulas()[0]; var values = sheet.getRange(rangeNotation).getValues()[0]; return formulas.reduce(function(acc, formula, i) { return acc += (/^=HYPERLINK/i.test(formula) && values[i] > 0 ? 1 : 0); }, 0); }
For this, how about this answer?
Issue and solution:
At May, 2020, it seems that the specification for using the hyperlinks in Google Spreadsheet was changed. By this, unfortunately, above script cannot be used now. But, in the current stage, the hyperlinks can be retrieved using Class RichTextValue. So, in the current stage, in order to achieve your goal, it is required to modify above script as follows.
Modified script:
function countLinks(rangeNotation) {
var sheet = SpreadsheetApp.getActiveSheet();
var richTextValues = sheet.getRange(rangeNotation).getRichTextValues();
return richTextValues.reduce((c, row) => {
row.forEach(col => {
col.getRuns().forEach(r => {
if (r.getLinkUrl()) c++;
});
});
return c;
}, 0);
}
When you use this script as the custom function, please put the following formula to a cell. In this case, the number of hyperlinks in the cells "A1:A10" are returned.
=countlinks("A1:C10")
Note:
- In this modified script, the number of hyperlinks both with and without using
HYPERLINK
can be retrieved. - Please use this script with V8.
References:
- How to extract the link from a cell now that links are not reflected as HYPERLINK?
- Class RichTextValue
- Unfortunately, in the current stage, it seems that the official document is still not updated. So the document of
getLinkUrl()
cannot be found. ButgetLinkUrl()
can be used.
- Unfortunately, in the current stage, it seems that the official document is still not updated. So the document of
Added:
When I saw your sample Spreadsheet, I could understand about the reason of your issue. The reason of your issue is due to that the number value is used for the hyperlink. In the current stage, it seems that getRichTextValues
cannot retrieve the number value. By this, only number of HYPERLINK
is retrieved. I think that this might be a bug. So I have already posted this issue to the issue tracker. Ref When this issue was resolved, I think that above sample script might work.
By the way, the cells which have the hyperlinks without the URL cannot be retrieved. Please be careful this.
Workaround:
As the current workaround, for example, when the cell format is changed from the number to the text, the count can be seen. But in this case, the cells which have the hyperlinks without the URL cannot be retrieved. Please be careful this.
来源:https://stackoverflow.com/questions/62245411/how-to-count-new-google-sheet-links