【问题标题】:Please assist with this if statement请协助执行此 if 语句
【发布时间】:2017-02-09 22:18:37
【问题描述】:

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

【问题讨论】:

  • 请正确描述您的问题。有关指导,请参阅 How to Askminimal reproducible example。如果您遇到错误,那么您应该将它们包含在您的问题中。
  • if (split === fiftyFifty); 什么都不做,去掉分号

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


【解决方案1】:

你是说这部分吗?

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

if(); 后面不需要分号

【讨论】:

    【解决方案2】:

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

      if (split === sixtyForty);
    

    应该是

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

    【讨论】:

      【解决方案3】:

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

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

      这是 if 语句的正确语法。

      【讨论】:

        【解决方案4】:

        你在 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
        }
        

        【讨论】:

          猜你喜欢
          • 2013-04-14
          • 1970-01-01
          • 1970-01-01
          • 2016-03-03
          • 1970-01-01
          • 1970-01-01
          • 2021-06-17
          • 1970-01-01
          相关资源
          最近更新 更多