I am trying to run a function on edit that will look for the value 'c' in a range of cells and then replace it with the word 'Closed'.
This is what I have so far:
function onEdit(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getRange('CA12:CA15').getValues();
var closed = 'c'
for(var i=0; i<data.length;i++) {
if(data[i] == closed) {
Logger.log('yes')
Logger.log(data[i]);
}
}
}
So this is successfully logging 'yes' and the value 'c' when I have this value within the given range. But how do I now replace this value?
I tried storing the range of data[i] using getRange() but it won't allow me to do this. If I can get this range then I know I can then use range.setValue('Closed') but a bit stuck at the moment. I know I'm doing something very simple very wrong but any help would be great.
I am trying to run a function on edit that will look for the value 'c' in a range of cells and then replace it with the word 'Closed'.
This is what I have so far:
function onEdit(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getRange('CA12:CA15').getValues();
var closed = 'c'
for(var i=0; i<data.length;i++) {
if(data[i] == closed) {
Logger.log('yes')
Logger.log(data[i]);
}
}
}
So this is successfully logging 'yes' and the value 'c' when I have this value within the given range. But how do I now replace this value?
I tried storing the range of data[i] using getRange() but it won't allow me to do this. If I can get this range then I know I can then use range.setValue('Closed') but a bit stuck at the moment. I know I'm doing something very simple very wrong but any help would be great.
Share Improve this question asked Nov 29, 2018 at 12:20 DevB1DevB1 1,5753 gold badges25 silver badges51 bronze badges3 Answers
Reset to default 5Key points:
getValues
returns a 2 dimensional array ordered by rows and then columns. You're accessing a 1D array withdata[i]
i
the index of array + start row is equal to the current row in loop.
Code snippet(slow):
if(data[i][0] == closed) { //2D
sheet.getRange('CA'+(12+i)).setValue('Closed');
}
- But the above is slow, because we're calling setValue() for each true condition. Better way is to use arrays.
Code snippet(fast 70x):
var rng =sheet.getRange('CA12:CA15');
var data = rng.getValues();
var output = data.map(function(e) { return e[0] == closed ? ['Closed'] : e });
rng.setValues(output); //Single call
- Fastest way is to use Find and Replace either from the sheets UI or API
Essential Reading:
- Best Practices
- Array#map
- 2D array
- FR API request
Try this:
function onEdit(e) {
var sheet = e.range.getSheet();
var editedCell=sheet.getRange(e.range.getRow(),e.range.getColumn()).getA1Notation();
var data = sheet.getDataRange().getValues();
var found=false;
var foundA=[];
for(var i=0;i<data.length;i++) {
if(data[i][2] == 'c') {
found=true;
foundA.push(i+1);
}
}
if(found){
Logger.log('Matches found in following rows of column C: %s\nCell Edited was: %s',foundA.join(','),editedCell);
}else{
Logger.log('No matches found');
}
}
For testing purposes you can use SpreadsheetApp.getUi().alert(Logger.getLog());
to display Logger results.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getRange('C2:C').getValues();
var name = valuetosearchincColumnC;
for(var i=0; i<data.length;i++) {
if(data[i] == name) {
var id = i + 2;// plus two bec i have a header
sheet.getRange(id,2).setValue('ValuetoChange');
}
}