【问题标题】:function with undefined parameters带有未定义参数的函数
【发布时间】:2012-09-12 17:15:05
【问题描述】:

我正在使用日历电子表格。本文档使用背景颜色来指定不同的事件。例如会议是蓝色的,庭院是红色的。

我正在尝试创建一个(sum if bg color)方法,该方法查看谷歌电子表格中的给定范围,计算具有特定背景颜色的单元格,为每个块分配权重(即 30 分钟)并返回在该背景颜色的活动/周上花费的时间。

我的基本问题是我的参数是未定义的(即使它们是范围),并且不能对未定义的对象类型调用范围方法。

代码如下:

    //this function sums cells of a given color having assigned each cell a given value.
    //it inspects a range and compares each cell's background color to a given background color
    //returns given title and calculated sum
    function sumifcolor(theRangeToInspect, valueOfCellInRangeToInspect, colorAndTitleCell) {
        var doc = SpreadsheetApp.getActiveSheet();
        var color = colorAndTitleCell.getBackgroundColor(); //get color
        var title = colorAndTitleCell.getInputvalueOfCellInRangeToInspect(); //get title
        theRangeToInspect.activate();
        var firstRow = theRangeToInspect.getRow();
        var lastRow = theRangeToInspect.getLastRow();
        var firstCol = theRangeToInspect.getColumn();
        var lastColumn = theRangeToInspect.getLastColumn();
        var result;

        //for loops iterate through every cell in a row
        for (var yy=firstRow; xx<=lastRow; xx++) {
           for (var xx=firstCol; yy<=lastCol; yy++) {
                if (doc.getRange(xx,yy).getBackgroundColor() = color) {result += valueOfCellInRangeToInspect} 
           };
        };
      return title + ": " + result;    
    };

【问题讨论】:

  • theRangeToInspect 未定义时,您希望函数做什么?

标签: google-apps-script google-sheets google-spreadsheet-api


【解决方案1】:

我假设您正在尝试将函数用作电子表格中的公式。

在这种情况下,不会将范围对象传递给函数,而是将范围内的值传递给函数。因此,

theRangeToInspect.getRow();

不会工作。 theRangeToInspect 将包含范围的内容。

【讨论】:

  • 你猜对了。如果一个范围没有传递给函数,它是作为二维数组发送的吗?并且该数组是否包含单元格和文本格式,或者只是单元格值作为字符串/数字?谢谢斯里克。
  • 不幸的是,没有传递给函数的格式,只有值。是的,所有这些值都作为 2 数组传递
猜你喜欢
  • 2011-06-06
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
相关资源
最近更新 更多