最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get range and then set value in Google Apps Script - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 5

Key points:

  • getValues returns a 2 dimensional array ordered by rows and then columns. You're accessing a 1D array with data[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');
    }
  }
发布评论

评论列表(0)

  1. 暂无评论