2013-05-16 229 views
2

我在線發現了一個腳本,它將當前表單複製到臨時新電子表格,將其轉換爲PDF並通過電子郵件發送給它。我能夠得到它的工作,但試圖設置它,以便它只發送一定範圍。試圖玩一下,但我不是一個很好的編碼器。或者,我也有興趣瞭解如何使它適合橫向1頁的PDF格式,無網格轉換(我的在線研究顯示這不可能?),甚至可以作爲XLS發送。Google Apps腳本通過電子郵件發送活動電子表格

// Simple function to send Weekly Status Sheets to contacts listed on the "Contacts" sheet in the MPD. 

// Load a menu item called "Project Admin" with a submenu item called "Send Status" 
// Running this, sends the currently open sheet, as a PDF attachment 
function onOpen() { 
    var submenu = [{name:"Send Status", functionName:"exportSomeSheets"}]; 
    SpreadsheetApp.getActiveSpreadsheet().addMenu('Project Admin', 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 = "Please see attached"; // Could make it a pop-up perhaps, but out of wine today 

    // Get Project Name from Cell A1 
    var projectname = originalSpreadsheet.getRange("A1:A1").getValues(); 
    // Get Reporting Period from Cell B3 
    var period = originalSpreadsheet.getRange("B3:B3").getValues(); 
    // Construct the Subject Line 
    var subject = projectname + " - Weekly Status Sheet - " + period; 


    // Get contact details from "Contacts" sheet and construct To: Header 
    // Would be nice to include "Name" as well, to make contacts look prettier, one day. 
    var contacts = originalSpreadsheet.getSheetByName("Contacts"); 
    var numRows = contacts.getLastRow(); 
    var emailTo = contacts.getRange(2, 2, numRows, 1).getValues(); 

    // Google scripts can't export just one Sheet from a Spreadsheet 
    // So we have this disgusting hack 

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

    // Find and delete the default "Sheet 1", after the copy to avoid triggering an apocalypse 
    newSpreadsheet.getSheetByName('Sheet1').activate(); 
    newSpreadsheet.deleteActiveSheet(); 

    // Make zee PDF, currently called "Weekly status.pdf" 
    // When I'm smart, filename will include a date and project name 
    var pdf = DocsList.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes(); 
    var attach = {fileName:'Weekly Status.pdf',content:pdf, mimeType:'application/pdf'}; 

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

    // Delete the wasted sheet we created, so our Drive stays tidy. 
    DocsList.getFileById(newSpreadsheet.getId()).setTrashed(true); 
} 

回答

0

爲了拆分電子表格,你必須首先將部分複製到一個臨時表,然後複製成片新的電子表格。

下面是我的代碼,用於將您的「創建電子表格...」替換爲刪除新電子表格Sheet1的位置。

//variables firstRow & lastRow define the part of the sheet to copy 
    //create new spreadsheet 
    var newSpreadsheet = SpreadsheetApp.create('Spreadsheet to export'); 
    //create temporary sheet to copy to new spreadsheet 
    var tempSheet = originalSpreadsheet.insertSheet(); 
    //if header copy it 
    if (sheet.getFrozenRows() > 0) 
    { 
    dataSheet.getRange(1, 1, sheet.getFrozenRows()).copyTo(tempSheet.getRange(1,1)); 
    } 
    //copy relevant data to temporary sheet 
    dataSheet.getRange(firstRow, 1, lastRow - firstRow + 1).copyTo(tempSheet.getRange(sheet.getFrozenRows() + 1, 1)); 
    //copy temp sheet to new spreadsheet 
    tempSheet.copyTo(newSpreadsheet); 

    //delete Sheet1 in new spreadsheet 
    newSpreadsheet.getSheetByName('Sheet1').activate(); 
    newSpreadsheet.deleteActiveSheet(); 

    //delete temp sheet 
    originalSpreadsheet.setActiveSheet(tempSheet); 
    originalSpreadsheet.deleteActiveSheet(); 
0

要發送您可以在發送之前隱藏所有其他的單張紙。

var sheet_to_send = 'Sheet1'; 
//----------------------------- 
var as = SpreadsheetApp.getActiveSpreadsheet(); 
var sheets = as.getSheets(); 
for(var i in sheets){ 
    if (sheets[i].getName()!=sheet_to_send){ 
     sheets[i].hideSheet(); 
    } 
} 
MailApp.sendEmail(email_to, email_subject, email_body, {attachments: SpreadsheetApp.getActiveSpreadsheet()}); 
for(var i in sheets){ 
    if (sheets[i].getName()!=sheet_to_send){ 
     sheets[i].showSheet(); 
    } 
} 

如果您有更多的張派,你會用JavaScript對象進行篩選:

var sheets_to_send = {'Sheet1':1, 'Sheet3': 1}; 
... 
// replace 
if (sheets[i].getName()!=sheet_to_send) 
// by 
if (!(sheets[i].getName() in sheet_to_send)) 
1

我使用這個代碼,以pdf格式自動發送一個谷歌電子表格的電子郵件。它工作正常,但我需要通過刪除網格線並設置顯示A4來自定義pdf。有沒有辦法?謝謝

Simple function to send Weekly Status Sheets to contacts listed on the "Contacts" sheet in the MPD. 

// Load a menu item called "Project Admin" with a submenu item called "Send Status" 
// Running this, sends the currently open sheet, as a PDF attachment 
function onOpen() { 
var submenu = [{name:"Invia", functionName:"exportSomeSheets"}]; 
SpreadsheetApp.getActiveSpreadsheet().addMenu('Invia PDF', 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 = "Messaggio email"; 
// Get Project Name from Cell A1 
var projectname = originalSpreadsheet.getRange("A1:A1").getValues(); 
// Get Reporting Period from Cell A2 
var period = originalSpreadsheet.getRange("A2:A2").getValues(); 
// Construct the Subject Line 
var subject = projectname; 


// Get contact details from "Contacts" sheet and construct To: Header 
// Would be nice to include "Name" as well, to make contacts look prettier, one day. 
var emailTo = originalSpreadsheet.getRange("A3:A3").getValues(); 


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

var aliases = GmailApp.getAliases() 
Logger.log(aliases); 
Logger.log(aliases[2]); //returns the alias located at position 0 of the aliases array 


// Make zee PDF, currently called "Weekly status.pdf" 
// When I'm smart, filename will include a date and project name 
var pdf = DocsList.getFileById(originalSpreadsheet.getId()).getAs('application/pdf').getBytes(); 
var attach = {fileName:'Nome_allegato',content:pdf, mimeType:'application/pdf'}; 

// Send the freshly constructed email 
GmailApp.sendEmail(emailTo, subject, message, {'from': aliases[2], attachments:[attach]}); 

// Delete the wasted sheet we created, so our Drive stays tidy. 
DocsList.getFileById(newSpreadsheet.getId()).setTrashed(true); 
} 
相關問題