How to loop getRange(“B” + row + “ :” + “J” + row) in google sheet editor script

爷,独闯天下 提交于 2020-01-26 04:55:25

问题


function onEdit(evt) {

var range = evt.range;

showAutoValue(range);

}

function showAutoValue(range){

  var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();

  var activeSheet = spreadSheet.getActiveSheet();

  var row = range.getRow();

  var column = range.getColumn();

  if(column == 1 && row >= 1){

    var Avalue = activeSheet.getRange("A" + row).getValue();

    var B_Jrange = activeSheet.getRange("B" + row + ":" + "J" + row);

    if(String(Avalue).trim()){
      for(var i = 0; i < B_Jrange.length; i++){
        if(String(B_Jrange[i].getValue()).trim().toLowerCase() == "change moi"){
          B_Jrange[i].setBackground(Avalue);
        }
      }
    }
  }

}

回答1:


Selecting and Highlighting Ranges with onEdit()

Okay I thought about this a bit more and I decided to make columnA a data validation list of white,yellow,lightgreen,lightblue because you must keep in mind that in order to generate the onEdit trigger you must actually change something. So in this case you're changing the background color of the range e.range.getSheet().getRange(e.range.rowStart,2,1,8) to the value that's selected in column A of that row. So no loop is required to accomplish this.

function onEdit(e){
  var sh=e.range.getSheet();
  if(e.range.columnStart==1){
    if(String(sh.getRange(e.range.rowStart,1).getValue()).trim()){
      sh.getRange(e.range.rowStart,2,1,8).setBackground(sh.getRange(e.range.rowStart,1).getValue()); 
    }else{
      sh.getRange(e.range.rowStart,2,e.range.rowEnd,8).setBackground('white').getValue(); 
    }
  }
}

This version also clears the entire range if you select all of column A and hit return it sets column A to no selection and the second part of the if/else statement clears background of the entire range.



来源:https://stackoverflow.com/questions/58696784/how-to-loop-getrangeb-row-j-row-in-google-sheet-editor-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!