无法将[object Object]转换为(class)

时间:2019-07-08 16:55:16

标签: javascript google-apps-script google-sheets

我正在尝试计算包含Google表格中特定单词的两行之间的行数。但是我收到以下错误: 无法将[object Object]转换为(类)。 (第41行,文件“代码”)

我已经在Google应用脚本上编写了以下代码:

function search(SPREADSHEET_ID, SHEET_NAME, word) {
    var locatedCells = [];
    var ss = SpreadsheetApp.openById(SPREADSHEET_ID);
    var searchLocation = ss.getSheetByName(SHEET_NAME).getDataRange().getValues();        
    //Loops to find the search term. 
        for (var j = 0, jLen = searchLocation.length; j < jLen; j++) {
            for (var k = 0, kLen = searchLocation.length; k < kLen; k++) {
                var find = word;
                if (find == searchLocation[j][k]) {
                    locatedCells.push({ 'found': (j + 1)});
                }
            }
        }
     //   Logger.log(locatedCells);
        return(locatedCells)
    }


 function footerlocation(){
  var SPREADSHEET_ID = "1nYBEuMMC4j1A4qryzKKq33PsTRH54ADyJLsEoTmbKh4"
  var SHEET_NAME = "Bedding"
  var word = "Footers"
  var footerlocation = search(SPREADSHEET_ID, SHEET_NAME, word)
  //Logger.log(footerlocation);
  return(footerlocation)
}


 function keywordlocation(){
  var SPREADSHEET_ID = "1nYBEuMMC4j1A4qryzKKq33PsTRH54ADyJLsEoTmbKh4"
  var SHEET_NAME = "Bedding"
  var word = "Keyword Page Redirects to Implement"
  var keywordlocation = search(SPREADSHEET_ID, SHEET_NAME, word)
  //Logger.log(keywordlocation);
  return(keywordlocation)
}

function count(){
  var sheet= SpreadsheetApp.openById("1nYBEuMMC4j1A4qryzKKq33PsTRH54ADyJLsEoTmbKh4").getSheetByName("Bedding");
  var startrow=footerlocation()
  var endrow= keywordlocation()
  var range = sheet.getRange(startrow,1,endrow-startrow,1);
  var datas = range.getValues();
  var count = 0;
  for (data in datas) {
      for (cell in data) {
           //Logger.log(typeof cell) //{
            count++;
          //}
  }

 }
Logger.log(data)



}

如果有人可以帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我能够找出解决方案,将count函数更改为以下内容:

function count(){
  var sheet= SpreadsheetApp.openById("1nYBEuMMC4j1A4qryzKKq33PsTRH54ADyJLsEoTmbKh4").getSheetByName("Bedding");
  var startrow=footerlocation()[0]['found']
  var endrow= keywordlocation()[0]['found']
  var range = sheet.getRange(startrow,1,endrow-startrow,1);
  var datas = range.getValues();
  var count = 0;
  for (data in datas) {
      for (cell in data) {
           //Logger.log(typeof cell) //{
            count++;
          //}
  }

 }
Logger.log(data)

}