请协助处理此if语句

时间:2017-02-09 22:18:38

标签: javascript google-apps-script google-sheets

此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(); }

4 个答案:

答案 0 :(得分:0)

你的意思是这部分吗?

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

if();

之后,您不需要分号

答案 1 :(得分:0)

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

  if (split === sixtyForty);

应该是

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

} 

答案 2 :(得分:0)

条件语句后面有分号。这终止了表达。

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

这是if语句的正确语法。

答案 3 :(得分:0)

Javascript中的if / else语句必须看起来像

if (condition) {

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

或者,如果您想使用其他,请执行以下操作:

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
}