2017-03-26 82 views
1

我卡住了,找不到我的問題的答案。getSheetByName谷歌應用程序腳本返回空

這裏是我當前的代碼:

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

monthSheet是字符串
destsheet總是,我不知道爲什麼...

編輯:

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  
{ 
} 

} }

+0

該代碼看起來沒問題。請提供一個示例文件。 –

+0

你能告訴我們'射擊'是如何定義的嗎? –

+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的值,同時是目標表單上的表單名稱。檢查你自己。

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);  
     } 
    } 
} 
0

感謝您的幫助。表單的正確名稱存在問題。 我也刪除了條件中的單引號。一切工作正常。

相關問題