首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无限地执行google应用程序脚本中的代码

无限地执行google应用程序脚本中的代码
EN

Stack Overflow用户
提问于 2016-04-27 08:55:13
回答 1查看 669关注 0票数 0

我有这个代码可以无限地运行。我一步一步地检查,错误在它的最后一部分,其中有一个循环。

我有细胞(f,3)值为400,在(f,4)细胞(f-1,4)胞(f-2,4)胞(f-3,4):200,50,20,100。

单元格(f,12)应该显示值270。

代码语言:javascript
复制
function myFunction() {

      var ss = SpreadsheetApp.getActiveSpreadsheet();      

      var sheet = ss.getSheetByName("Feuille 1");
      var active = sheet.getActiveCell();

      var f = active.getRowIndex();           
      var r = sheet.getRange(f,3).getValues();        
      var i = f          
      var cellule = sheet.getRange(i,4).getValues();      
      var C = 0

      do {            
        C = C + cellule;
        var destverif = sheet.getRange(f,12);      
        destverif.setValue(C);
        i = i-1;            
         }while (C + cellule <= r);      

 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-27 10:26:20

您使用的是getValues(),而不是返回数组的getValue()。虽然您的时间条件本质上是检查while(NaN + [["200"]] <= [["400"]]),但这并不能真正解决问题,看起来它似乎返回了true,并且您有一个本质上是while(true)的循环。

若要更正此问题,请改用getValue()。这立即纠正了无限循环。

我在gchat上和您聊天,并确定您的逻辑对于您期望的输出也是错误的。您正在尝试获取这样一组数据:

200开始,获取突出显示的列的值,直到总数大于或等于400为止。一旦它到达此值,将写入该行400 / AVERAGE(200,100,101)的第12列。

我更正了你的代码,这段代码的作用是:

代码语言:javascript
复制
function getValues(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Feuille 1");
  var active = sheet.getActiveCell();  

  var rowIndex = active.getRowIndex(); //Get the rowindex of the current active cell
  var maxValue = sheet.getRange(rowIndex,3).getValue(); //Your max value is the active cells value

  var currentValue = 0;
  var activeRowIndex = rowIndex
  var loopIndex = 0;
  do{

    var cellData = sheet.getRange(activeRowIndex, 4).getValue(); //Get the value of the cell next to the selectedCell
    if(cellData !== ''){ //If the cell is not blank continue
      currentValue += cellData; //Add up the total as we go
      activeRowIndex --; //Subtract the row index so we can keep movign up
      loopIndex++; //Iterate the loopIndex so we know how many numbers we have added up to calculate the average
    } else {      
      break; //If the cell is blank, break out of the loop
    }

  } while (currentValue <= maxValue);

  sheet.getRange(rowIndex, 12).setValue(maxValue/(currentValue/loopIndex)); //set the ranges value and do the math in line for the average.
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36884926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档