getSheetByName google apps脚本返回null

时间:2017-03-26 15:53:31

标签: google-apps-script google-sheets

我被困住了,无法找到问题的答案。

这是我目前的代码:

var monthSheet = sourcesheet.getRange('A1').getValue();
var destsheet = shoot.getSheetByName(monthSheet);

monthSheet是字符串
destsheet总是 null ,我不知道为什么......

编辑:

function sendAnnouncement() {



 var files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxx").getFiles()
    while (files.hasNext()) {
      var file = files.next();
      var shoot = SpreadsheetApp.openById(file.getId());


  var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Announcements");
  var sourceAnn = sourcesheet.getRange('B2:B2').getValue();

  var sourceMonth = sourcesheet.getRange('A2').getValue();
  sourcesheet.getRange('A1').setFormula("=VLOOKUP(A2;Settings!B1:C12;2;false)");
  var monthSheet = sourcesheet.getRange('A1').getValue();
  var sourceTaxStake = sourcesheet.getRange('C2').getValue();


 var destsheet = shoot.getSheetByName(monthSheet);
 var destTaxStake = destsheet.getRange('H2').getValues();  

  if ('sourceTaxStake' == "all")
  {
var destrange = destsheet.getRange('D8:D8'); 
destrange.setValues(sourceAnn);     
}
  else if ('destTaxStake' == 'sourceTaxStake')
  {
var destrange = destsheet.getRange('D8:D8'); 
destrange.setValues(sourceAnn);     
}
else    
{
}

} }

3 个答案:

答案 0 :(得分:0)

同样,代码本身的那部分是可以的。你检查了monthSheet的值是多少?可能它与实际的工作表名称不同。同时按Logger.log(monthSheet)检查其值。查看我的示例source sheetdestination sheet。下面的脚本包含在源表中。

function myFunction() {
  var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Announcements");
  var monthSheet = sourcesheet.getRange('A1').getValue();

  var shoot = SpreadsheetApp.openById('1JxjM7KJRofTRxH5pRpY4vLuY9ojKP__DYjodSIN3rVE');
  var destsheet = shoot.getSheetByName(monthSheet);
  Logger.log(destsheet.getName());  // dest_s
}

当然1JxjM7KJRofTRxH5pRpY4vLuY9ojKP__DYjodSIN3rVE是我的目标表。 dest_s是源表上Announcements!A1的值,同时也是目标表上的表单名称。自己检查一下。

答案 1 :(得分:0)

I noticed that sourceTaxStake and destTaxStake had single quotes around them so  neither condition would ever by true.  I removed the single quotes and the editor immediately darkened them as it does with defined variables.

function sendAnnouncement() 
{
 var files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxx").getFiles()
    while (files.hasNext()) 
    {
      var file = files.next();
      var shoot = SpreadsheetApp.openById(file.getId());
      var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Announcements");
      var sourceAnn = sourcesheet.getRange('B2').getValue();
      var sourceMonth = sourcesheet.getRange('A2').getValue();
      sourcesheet.getRange('A1').setFormula("=VLOOKUP(A2;Settings!B1:C12;2;false)");
      var monthSheet = sourcesheet.getRange('A1').getValue();
      var sourceTaxStake = sourcesheet.getRange('C2').getValue();
      var destsheet = shoot.getSheetByName(monthSheet);
      var destTaxStake = destsheet.getRange('H2').getValues();  
      if (sourceTaxStake == "all")
      {
        var destrange = destsheet.getRange('D8:D8'); 
        destrange.setValues(sourceAnn);     
      }
      else if (destTaxStake == 'sourceTaxStake')
      {
        var destrange = destsheet.getRange('D8:D8'); 
        destrange.setValues(sourceAnn);     
      }
    } 
}

答案 2 :(得分:0)

感谢您的帮助。工作表的名称有问题。 我还从条件中删除了单引号。一切都正常。