简体   繁体   English

如何在Google App脚本中求和范围列

[英]How sum a range column in google app script

J    
1    
2    
3    

This is the code: 这是代码:

function sumsald () {
   var ss = SpreadsheetApp.getActiveSpreadsheet ();
   var s1 = ss.getSheetByName ("rendPendForm");
   var dataRange = s1.getDataRange ();
   var lastrow = dataRange.getLastRow ();
   var values = s1.getRange (2.10, lastrow, 1) .getValues ();
        var sum = 0;           
        for (var i = 0; i <values.length; i ++) {
            result = values [i] [10] + sum;
            Logger.log (result);
     }
     s1.getRange (i + 1, 10) .setValue (result);
  }

Result script = # NUM! 结果脚本= # NUM!
Logger.log = NaN Logger.log = NaN

6 expected 预期6

Thanks, Luis. 谢谢,路易斯。

Not sure if in the example the J is the column name or value on the first row. 不知道示例中的J是否是第一行的列名或值。 Anyway, to get values starting from first row in column J all the way to row on index lastrow : 无论如何,要获取从J列的第一行开始一直到索引lastrow行的值:

var values = s1.getRange (1, 10, lastrow, 1) .getValues ();

In addition, when adding up the numbers its good to ignore non-number values. 另外,在将数字相加时,忽略非数字值是有好处的。 For example if you sum a string from a header row and a number from row below you get a concatenated string of the two in Javascript/GoogleScript, instead of an error or ignoring the string. 例如,如果您将标题行中的字符串与下面一行中的数字相加,则会在Javascript / GoogleScript中获得两者的串联字符串,而不是错误或忽略该字符串。 This appends to the sum only if values[i][0] contains a number (otherwise adds 0 to the sum ie does nothing): 仅当values[i][0]包含数字时,才将其追加到总和上(否则将0加到总和上,即不执行任何操作):

result += typeof values[i][0] == 'number' ? values[i][0] : 0;

Below full example: 下面是完整的示例:

function sumsald () {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var s1 = ss.getSheetByName ("rendPendForm");
  var dataRange = s1.getDataRange ();
  var lastrow = dataRange.getLastRow ();
  var values = s1.getRange (1, 10, lastrow, 1) .getValues ();
  var result = 0;
  for (var i = 0; i <values.length; i ++) {
    result += typeof values[i][0] == 'number' ? values[i][0] : 0;
    Logger.log (result);
  }
  s1.getRange (i + 1, 10) .setValue (result);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Google App Script设置范围直到工作表中列的最后一行 - How to set the range until last row of a column in sheet using Google App Script Google应用脚本可从同一电子表格创建多个图表,但不同列的范围不同 - Google app script to create Multiple Charts & Tables from same spreadsheet but different range from different column 查找范围内值的匹配项并获取每个匹配项的第一个单元格和第一列值谷歌应用脚本 - Find matches to a value in a range and get first cell and first column values for each match google app script Google App脚本中的范围宽度不正确 - Incorrect range width in Google App Script 在谷歌应用脚​​本中获取“不正确的范围高度” - Getting “Incorrect range height” in google app script Google App脚本-设置范围内的公式 - Google App Script - set formulas over range 谷歌应用脚​​本。 检查 Range1 是否与 Range2 相交 - Google App Script. Check if Range1 intersects Range2 如何使用带有 Google Apps 脚本的主列表更新特定列中特定范围的数据验证? - How to update data validation for a certain range within a specific column with a master list with Google Apps Script? 如何让 Google 脚本在特定行范围内的特定列中隐藏带有空白或零的行? - How do I get Google Script to hide rows with blanks or zeroes in a specific column, within a specific range of rows? Google App Script - 将数据插入下一列 - Google App Script - Insert Data Into Next Column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM