Script to return a value on a specific cell if other cell content meets criteria

孤者浪人 提交于 2019-12-11 13:51:33

问题


I'm just getting started with Google scripts and I'm trying to create a very basic script to help me understand the fundamentals.

The script would look for the number 5 in cell E2 and then return a value of 5 if true or 10 if false. The issue I'm running into is that if I enter a 5 on E2 I do not get a 5 on E4. it always says 10 on E4.

I have tried changing the operator to = and === as well as putting quotes ("5") around the 5 to look for text rather than a number and can't get it to work.

function onEdit()
{

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getRange(2,5);
var cell2 = sheet.getRange(4,5);


  if (cell == 5) {cell2.setValue(5);}
  else {cell2.setValue(10);}


}

回答1:


The mistake is in comparison cell == 5. It should be cell.getValue() == 5.

Indeed, cell is an object that abstractly represents the cell: by calling one its methods, you can change its value, or make the background red, or set data validation, etc. getValue is one of these methods, which returns the value contained in the cell.



来源:https://stackoverflow.com/questions/36458654/script-to-return-a-value-on-a-specific-cell-if-other-cell-content-meets-criteria

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