单元格引用超出范围-但我使用GetDataRange()

时间:2019-07-12 15:57:01

标签: google-apps-script google-sheets

我尝试了很多解决方案,但是总是以Cell ref超出范围为结束。

我认为GetDataRange()GetLastRow()可以覆盖最后一个非空白的单元格,但我陷入了困境。我试图对单元格值(从X到Y)进行硬编码,但似乎没有任何变化。

var course; 
var searchingCol; 

function CalculateFurnitureList()
{
  GetCourseString(); 
  FindColumnNumber(course); 
  GetSpecialFurnituresValue(); 
}

function GetCourseString()
{
  var activeSheet = SpreadsheetApp.getActiveSheet();
  var courseType = activeSheet.getRange("A2").getValue();
  Browser.msgBox('Generate furniture list for ' + courseType + ' course');

  course = courseType;  
}

function FindColumnNumber(ct)
{
  var furnitureSheet = SpreadsheetApp.getActive().getSheetByName("MaterielCours"); 
  var range = furnitureSheet.getDataRange(); 
  var width = range.getWidth();

  //Search every cell in the designated area to find the course name
  for (var i = 1; i <= width; i++)
  {
    var data = range.getCell(1,i); 
    if (data.getValue() == ct)
    {
      Browser.msgBox('Trouvé ! ' + data.getValue() + ' se trouve en : ' + i +':1');
      searchingCol = i; 
    }
  }
}

function GetSpecialFurnituresValue()
{
  Browser.msgBox(searchingCol); 

  var furnitureSheet = SpreadsheetApp.getActive().getSheetByName("MaterielCours"); 
  var range = furnitureSheet.getDataRange();
  var length = range.getLastRow();

  for (var i = 1; i < length; i++)
  {
    var data = range.getCell(searchingCol, i); 
    if (data.getValue() !== "")
    {
      // Do nothing
    }
    else
    {
      if (data == "Abs")
      {
        Browser.msgBox('NTM');
      }
      else
      {
        // Calculate value
      }
    }
  }
}

var data = range.getCell(searchingCol, i); GetSpecialFurnituresValue()中导致问题的原因。

奇怪的是,FindColumnNumber中的同一过程运行良好。

1 个答案:

答案 0 :(得分:2)

(编辑我的原始答案-我一定很累!)在函数GetSpecialFurnitureValues中,有几件事。

for循环使用i <长度,但应为<=,否则您将错过最后一行:

import typing


class A:
    def __init__(self, a):
        self.a = a

    def is_b(self):
        return isinstance(self, B)

    def is_c(self):
        return isinstance(self, C)


class B(A):
    def __init__(self, a, b):
        self.b = b

        super().__init__(a)


class C(A):
    def __init__(self, a, c):
        self.c = c

        super().__init__(a)


a_list: typing.List[A] = []
for i in range(0, 10):
    b_or_c: A
    if i % 2 == 0:
        b_or_c = B('a' + str(i), 'b' + str(i))
    else:
        b_or_c = C('a' + str(i), 'c' + str(i))

    a_list.append(b_or_c)

for b_or_c in a_list:
    print(type(b_or_c))
    if b_or_c.is_b():
        print(b_or_c.b)  # Mypy Error: "A" has no attribute "b"

    if b_or_c.is_c():
        print(b_or_c.c)  # Mypy Error: "A" has no attribute "c"

    if isinstance(b_or_c, B):
        print(b_or_c.b)  # No Mypy Error

    if isinstance(b_or_c, C):
        print(b_or_c.c)  # No Mypy Error

您的列/行仅被反转。 :)

// for (var i = 1; i < length; i++)  
for (var i = 1; i <= length; i++)

希望有帮助!