问题
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