我有这个代码可以无限地运行。我一步一步地检查,错误在它的最后一部分,其中有一个循环。
我有细胞(f,3)值为400,在(f,4)细胞(f-1,4)胞(f-2,4)胞(f-3,4):200,50,20,100。
单元格(f,12)应该显示值270。
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);
}发布于 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列。
我更正了你的代码,这段代码的作用是:
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.
}https://stackoverflow.com/questions/36884926
复制相似问题