为了账号安全,请及时绑定邮箱和手机立即绑定

导致错误的空 Google WorkSheet:异常:范围内的列数必须至少为 1

导致错误的空 Google WorkSheet:异常:范围内的列数必须至少为 1

潇湘沐 2022-12-29 09:40:51
我的 Google App 脚本将遍历所有 WorkSheets 以查找特定字符串并输出找到它的行和列,但是只要我添加一个新的空 WorkSheet 我就会收到错误“异常:范围内的列数必须至少为 1。” 我想了解导致此问题的原因。谢谢你。function doForAllSheets(){ var spreadsheet = SpreadsheetApp.getActive(); var allSheets = spreadsheet.getSheets();    allSheets.forEach(function(sheet){    if(sheet.getSheetName() !== "Main"){    sheet.activate();     findValueInRange();        }  })}                     function findValueInRange () {  var ss = SpreadsheetApp.getActiveSheet(),      theLastColumn = ss.getLastColumn(),      theLastRow = ss.getMaxRows();  //Returns a two dimensional array of both rows and columns  var values = ss.getRange(1,1,theLastRow,theLastColumn).getValues();  var i=0,      rownum=0,       thisRow,      colvalue,      whatToFind = "testserial",      j=0;  for(i=0;i<values.length;i++) {    thisRow = values[i];    for (j=1;j<thisRow.length;j++) {      colvalue = thisRow[j];      if (colvalue === whatToFind) {        Logger.log("The string is in row: " + (i + 1) + " and column: " + (j+1));      }    }  }}
查看完整描述

1 回答

?
浮云间

TA贡献1829条经验 获得超4个赞

像这样重写函数:


function findValueInRange () {

  var ss = SpreadsheetApp.getActiveSheet(),

      theLastColumn = ss.getLastColumn(),

      theLastRow = ss.getMaxRows();

  if( theLastColumn>0) {

  //Returns a two dimensional array of both rows and columns

  var values = ss.getRange(1,1,theLastRow,theLastColumn).getValues();


  var i=0,

      rownum=0, 

      thisRow,

      colvalue,

      whatToFind = "testserial",

      j=0;


  for(i=0;i<values.length;i++) {

    thisRow = values[i];


    for (j=1;j<thisRow.length;j++) {

      colvalue = thisRow[j];

      if (colvalue === whatToFind) {

        Logger.log("The string is in row: " + (i + 1) + " and column: " + (j+1));

      }

    }

  }

  }

}


查看完整回答
反对 回复 2022-12-29
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号