请协助这个if语句

问题描述 投票:-5回答:4

此googlescript代码中的if语句应返回每个条件的某些值,但它无法正常运行。我也试过'else if',但不断收到错误消息。我是初学者,想要一些反馈意见。

function doGet() {



 var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1;         // First row of data to process
  var numRows = 10000;     // Number of total rows
  var startColumn = 1
  var numColumns = 17



// Fetch the range of cells A2:Q10001
    var dataRange = sheet.getRange(startRow, startColumn, numRows, numColumns);

  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var timeStamp = row[0];
    var purchaseDate = row[1];
    var month = row[2];
    var responsibleParty = row[3];
    var invoiceTo = row[4];
    var item = row[5];
    var ifOther = row[6]
    var category = row[7];
    var amount = row[8];
    var split = row[9];
    var comments = row[10];
    var splitOptions = row[11];
    var splitCalc = row[12];
    var costToResponsible = row[13];
    var costToOther = row[14];
    var emailSent = row[15];
    var totalBudget = row[16];

    var fiftyFifty = "BQ:SQ, 50:50";
    if (split === fiftyFifty);
    var calc2 = amount*.5;
    {sheet.getRange(startRow + i, 14).setValue(calc2);
    SpreadsheetApp.flush(); }

    var sixtyForty = "BQ:SQ, 60:40";
    if (split === sixtyForty);
    var calc3 = amount*.33;
    {sheet.getRange(startRow + i, 14).setValue(calc3);
    SpreadsheetApp.flush(); }

    var fortySixty = "BQ:SQ; 40:60";
    if (split === fortySixty);
    var calc1 = amount*.67;
    {sheet.getRange(startRow + i, 14).setValue(calc1);
     SpreadsheetApp.flush();}

    var Bqpays = "BQ pays total amt";
    var calc4 = "0";
    if (split === Bqpays);
    {sheet.getRange(startRow + i, 14).setValue(calc4);
    SpreadsheetApp.flush(); }

    var Sqpays = "SQ pays total amt";
    var calc5 = amount;
    if (split === Sqpays) 
    {sheet.getRange(startRow + i, 14).setValue(calc5);
    SpreadsheetApp.flush(); }
javascript google-apps-script google-sheets
4个回答
0
投票

你的意思是这部分吗?

if (split === Bqpays);
    {sheet.getRange(startRow + i, 14).setValue(calc4);
    SpreadsheetApp.flush(); }

if();之后你不需要分号


0
投票

语法错误,在if块后删除分号

  if (split === sixtyForty);

应该

if (split === sixtyForty){
 var calc3 = amount*.33;
 etc....

} 

0
投票

在条件语句之后你有分号。这终止了表达。

if (split === fiftyFifty){
    var calc2 = amount*.5;
    sheet.getRange(startRow + i, 14).setValue(calc2);
    SpreadsheetApp.flush(); 
}

这是if语句的正确语法。


0
投票

你在Javascript中的if / else语句必须是这样的

if (condition) {

    block of code to be executed if the condition is true
}

或者如果你想使用else,你可以这样做:

if (condition) {
    block of code to be executed if the condition is true
} else { 
    block of code to be executed if the condition is false
}
© www.soinside.com 2019 - 2024. All rights reserved.