问题
I have a range of cells in Google Sheets Some of these cells contain values (numbers) some with and others without HYPERLINKS I need to count the number of hyperlinks for this range of cells if the value > 0
This function works fine, but does not include any cell-values Counting hyperlinks on Google Sheets formula
回答1:
- You want to calculate the number of cells which have the formulas of
=HYPERLINK()
and the value more than0
.- This is run for one row and also there are the cells which have no hyperlink.
- You want to achieve this using the custom function.
- As a sample, in your shared Spreadsheet, you want the number of
2
.
I could understand about your goal like above. If my understanding is correct, how about this modification? Please think of this as just one of several answers.
Your current script:
From the shared Spreadsheet, I confirmed your current script is as follows.
function countLinks(rangeNotation, range) {
var formulas = SpreadsheetApp.getActiveSheet().getRange(rangeNotation).getFormulas();
return formulas.reduce(function(acc, row) {
return acc + row.reduce(function(acc, formula) {
return acc + (/^=HYPERLINK/i.test(formula) ? 1 : 0);
}, 0);
}, 0);
}
Modification points:
- In your current script, the values are not retrieved.
- In this modification, both the formulas and values are retrieved from the cells.
- In your case, the formulas are retrieved from one row. So one
reduce()
can be removed. range
is not used.
When above points are reflected to your current script, it becomes as follows.
Modified script:
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);
}
Usage:
When you use this as the custom function, for example, please put the following custom function to the cell "AO3" in your shared Spreadsheet.
=countlinks("A3:AN3")
Result:
When above script is used for your shared Spreadsheet, the following result is obtained.
References:
- getFormulas()
- getValues()
- reduce()
Updated: June 8, 2020:
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, 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);
}
References:
- How to count new Google sheet links
- How to extract the link from a cell now that links are not reflected as HYPERLINK?
来源:https://stackoverflow.com/questions/56807304/google-sheets-count-numbers-of-hyperlinks-where-value-0