I believe your goal as follows.
- You want to search the cells which has no value of
100 and put the different value to the searched cells. (As a test, I input 5 or 6 cells with different value.)
Modification points:
- In your script, at
var invalidNumber = notNumber100.push(col);, invalidNumber is the length of the array of notNumber100. And, the array of notNumber100 is redeclared every loop. By this, the array length is always 1. As the result, you see the value of 1 at the log. I think that this is the reason of your issue.
If you want to check the values of array and the cell coordinates, you can modify as follows.
Modified script 1:
In this modification, the active range is used. This is from your script.
function countValue() {
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveRange();
var startRow = range.getRow();
var startCol = range.getColumn();
var spreadsheet = range.getValues();
spreadsheet.forEach(function(row, i) {
row.forEach(function(col, j){
if (col != 100) {
var notNumber100 = [];
notNumber100.push(col);
Logger.log("row: %s, col: %s, valueOfnotNumber100: %s", startRow + i, startCol + j, notNumber100);
// range.offset(i, j, 1, 1).setValue(6); // When you use this line, the cell value is replaced with the value of `6`.
};
});
});
}
If you want to search all cells in a sheet, you can also use the following script.
function countValue() {
var range = SpreadsheetApp.getActiveSpreadsheet().getDataRange();
range.getValues().forEach(function(row, i) {
row.forEach(function(col, j){
if (col != 100) {
var notNumber100 = [];
notNumber100.push(col);
Logger.log("row: %s, col: %s, valueOfnotNumber100: %s", i + 1, j + 1, notNumber100);
// range.offset(i, j, 1, 1).setValue(6); // When you use this line, the cell value is replaced with the value of `6`.
};
});
});
}
Modified script 2:
For example, if you want to search the cells with no value of 100 and replace it with other value, I think that you can also use TextFinder. When TextFinder is used, the sample script is as follows and the process cost can be reduced a little.
function countValue() {
// Search the cells
var textFinder = SpreadsheetApp.getActiveSheet().createTextFinder("^(?!100).+$").useRegularExpression(true).matchEntireCell(true);
// Confirm the cell coordinates and the cell values.
textFinder.findAll().forEach(r => {
Logger.log("row: %s, col: %s, cellValue: %s", r.getRow(), r.getColumn(), r.getValue());
});
// Replace the searched cells with the value of `6`.
textFinder.replaceAllWith("6");
}
- In this case, when
var textFinder = SpreadsheetApp.getActiveSheet().createTextFinder("^(?!100).+$").useRegularExpression(true).matchEntireCell(true); is modified to var textFinder = SpreadsheetApp.getActiveSpreadsheet().createTextFinder("^(?!100).+$").useRegularExpression(true).matchEntireCell(true);, all sheets are used as the target sheets.
References: