2013-02-09 53 views
1

对于一般与Google Apps脚本和JavaScript相关的任何代码,我都是全新的。TypeError:无法在对象中找到函数getCell Sheet

我已经适应一个脚本来我的需求,但我收到以下错误,当我运行它:

TypeError: Cannot find function getCell in object Sheet

从本质上讲,我试图让小区D4的值(4,4)并将该值传递给变量emailTo。我显然做得不正确。脚本的其余部分应该可以正常工作。任何指导表示赞赏。

// Sends PDF receipt 
// Based on script by ixhd at https://gist.github.com/ixhd/3660885 

// Load a menu item called "Receipt" with a submenu item called "E-mail Receipt" 
// Running this, sends the currently open sheet, as a PDF attachment 
function onOpen() { 
    var submenu = [{name:"E-mail Receipt", functionName:"exportSomeSheets"}]; 
    SpreadsheetApp.getActiveSpreadsheet().addMenu('Receipt', submenu); 
} 

function exportSomeSheets() { 
    // Set the Active Spreadsheet so we don't forget 
    var originalSpreadsheet = SpreadsheetApp.getActive(); 

    // Set the message to attach to the email. 
    var message = "Thank you for attending ! Please find your receipt attached."; 

    // Construct the Subject Line 
    var subject = "Receipt"; 

    // THIS IS WHERE THE PROBLEM IS 
    // Pull e-mail address from D4 to send receipt to 
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
    var emailTo = sheet.getCell(4, 4).getValue(); 

    // Create a new Spreadsheet and copy the current sheet into it. 
    var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export"); 
    var projectname = SpreadsheetApp.getActiveSpreadsheet(); 
    sheet = originalSpreadsheet.getActiveSheet(); 
    sheet.copyTo(newSpreadsheet); 

    // Find and delete the default "Sheet 1" 
    newSpreadsheet.getSheetByName('Sheet1').activate(); 
    newSpreadsheet.deleteActiveSheet(); 

    // Make the PDF called "Receipt.pdf" 
    var pdf = DocsList.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes(); 
    var attach = {fileName:'Receipt.pdf',content:pdf, mimeType:'application/pdf'}; 

    // Send the constructed email 
    MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]}); 

    // Delete the wasted sheet 
    DocsList.getFileById(newSpreadsheet.getId()).setTrashed(true); 
} 
+0

使用sheet.getRange代替getCell。 – ScampMichael 2013-02-09 05:07:54

+2

在脚本编辑器中编写代码时,您可以使用自动完成功能(控制+空格)来避免此类错误,因为它会向您显示所有可能的和有效的可用方法。 – 2013-02-09 09:04:47

+0

使用'getRange(4,4,1,1)'或使用'getRange(“D4”)'。 – 2013-02-09 17:00:45

回答

0

的问题是,getCell()Range的方法,而不是Sheet。从Sheet获取Range,然后在Range对象上使用getCell()

+0

有关'Sheet'没有'getCell()'方法的一点是绝对正确的。 – Mogsdad 2015-11-26 20:52:47

相关问题