范围A4:A100无法与Google脚本中的.getRange一起使用

时间:2018-12-17 17:41:38

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

它已经与value = thisSheet.getRange("A4").getValue();一起使用,但是当我将其更改为"A4:A100"范围时却没有使用

这是我的代码:

function EqualValue() {
  var allSheetTabs,i,L,thisSheet,thisSheetName,sheetsToExclude,value;

  sheetsToExclude = ['Template','Summary','Count'];

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  allSheetTabs = ss.getSheets();

  L = allSheetTabs.length;

  for (i=0;i<L;i++) {
    thisSheet = allSheetTabs[i];
    thisSheetName = thisSheet.getName();

    //continue to loop if this sheet is one to exclude
    if (sheetsToExclude.indexOf(thisSheetName) !== -1) {continue;}

    value = thisSheet.getRange("A4:A100").getValue();
    if(value >= "15") sendEmail(value)
  } 

任何建议都值得欢迎

2 个答案:

答案 0 :(得分:1)

我运行了完全一样的功能,

function EqualValue() {
  var allSheetTabs,i,L,thisSheet,thisSheetName,sheetsToExclude,value;

  sheetsToExclude = ['CheckBoxes','Globals'];

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  allSheetTabs = ss.getSheets();

  L = allSheetTabs.length;

  for (i=0;i<L;i++) {
    thisSheet = allSheetTabs[i];
    thisSheetName = thisSheet.getName();

    //continue to loop if this sheet is one to exclude
    if (sheetsToExclude.indexOf(thisSheetName) !== -1) {continue;}

    value = thisSheet.getRange("A4:A100").getValue();
    Logger.log('%s-The value in %s is %s',i+1,thisSheet.getRange('A4:A100').getA1Notation(),value)
    if(value >= "4") Logger.log('%s-The value in %s is %s and it is greater than or equal to 4',i+1,thisSheet.getRange('A4:A100').getA1Notation(),value);
  }
}

它有效,这是我的Logger.log结果:

[18-12-17 11:59:24:195 MST] 3.0-The value in A4:A100 is 
[18-12-17 11:59:24:305 MST] 4.0-The value in A4:A100 is 
[18-12-17 11:59:24:380 MST] 5.0-The value in A4:A100 is 3.0
[18-12-17 11:59:24:459 MST] 6.0-The value in A4:A100 is 
[18-12-17 11:59:24:541 MST] 7.0-The value in A4:A100 is 4.0
[18-12-17 11:59:24:543 MST] 7.0-The value in A4:A100 is 4.0 and it is greater than or equal to 4
[18-12-17 11:59:24:616 MST] 8.0-The value in A4:A100 is 4.0
[18-12-17 11:59:24:618 MST] 8.0-The value in A4:A100 is 4.0 and it is greater than or equal to 4
[18-12-17 11:59:24:690 MST] 9.0-The value in A4:A100 is 

我用“ 4”代替“ 15”。我以为它将返回左上方的单元格,并且确实如此。因此,也许是您的sendEmail(value)函数。

答案 1 :(得分:0)

您说“列A是行”,但A是列。我假设您的意思是每个选项卡的A列都有值列表。还假定每天将一个新值添加到底部的列表(?)。当您将值与15进行比较时,您会发送sendEmail,但是您如何知道哪个选项卡?

我已经将选项卡名称添加到sendEmail函数中,您应该能够弄清楚这一点。

function EqualValue() {
  try {
    var sheetsToExclude = ['Template','Summary','Count'];
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var allSheetTabs = ss.getSheets();
    for( var i=0; i<allSheetTabs.length; i++ ) {
      var sheet = allSheetTabs[i];
      if( sheetsToExclude.indexOf(sheet.getName()) >= 0 ) continue;
      // I'm assuming you keep adding to A column and only want the last one?
      var values = sheet.getRange(sheet.getLastRow(),1,1,1).getValue();
      if( value >= 15 ) {
        sendEmail(sheet.getName(),value);  // You will get an email for every tab that passes test
      }
    }
  }
  catch(err) {
    SpreadsheetApp.getUi().alert(err);
  }
}