GoogleSheets onEdit()向下舍入该数字

时间:2017-02-08 05:12:34

标签: google-apps-script

我应该使用哪些脚本将输入数字舍入到特定单元格(N30)上最接近的4的倍数。我尝试过使用此代码,但它不起作用:

function onEdit() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = sheet.getActiveRange().getActiveCell("N30");
  return SpreadsheetApp.getActiveCell(). Math.Mround("N30,4");
}

1 个答案:

答案 0 :(得分:1)

试试这个:

 function onEdit() 
 {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  var num= sheet.getRange("N30").getValue();

  if((num%4)>2)
  {
    num = ((Math.floor((num/4))) + 1)*4;
  }
  else
  {
    num = Math.floor((num/4))*4;
  }

  sheet.getRange("N30").setValue(num);
 }