如果另外两列中的值相等,则在空白单元格中插入值

时间:2018-05-18 00:35:45

标签: google-apps-script google-sheets

尝试让脚本在Google工作表上运行,并且无法满足第二项要求。我从多个地方找到了这个脚本,并把它们放在一起,但是我可能不太了解它,无法正确调整它。我希望它做的是查看两个不同的列(水果和状态),如果它们具有特定值,则将值输入另一列。这是当前的脚本:

function myFunction() {
var activeSheet = SpreadsheetApp.getActiveSheet();
var sheetName = activeSheet.getSheetName();
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var range = sheet.getDataRange();
var fruit = sheet.getRange('a2:a').getValues();
var cost = sheet.getRange('c2:c').getValues();
var status = sheet.getRange('b2:b').getValues();
 for (var row in fruit)
  for (var col in fruit[row])
   if (fruit[row][0] == 'Apple' && status[row][0] == 'Ripe' ) {
    num = parseInt(row) + 1;
    sheet.getRange('c' + num).setValue('5.5');
  }
}

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

我这样做:

function yourFunction(){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet3');
  var rg=sh.getDataRange();//columns are fruit,status and then cost.
  var vA=rg.getValues();
  for(var i=1;i<vA.length;i++){
    if(vA[i][0].toString().toLowerCase()=='apple' && vA[i][1].toString().toLowerCase()=='ripe'){
      vA[i][2]=5.5;//write back into array and write all of them at one time after loop.
    }
  }
  rg.setValues(vA);//This writes all of the data at one time.
}

这就是我的电子表格:

enter image description here