function onOpen() {
var submenu = [{name:"einreichen", functionName:"sendEmailWithPdfAttach"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu('E-mail senden', submenu);
}
var range = 'A1:J23'
var source = SpreadsheetApp.getActiveSpreadsheet().getRange(range);
var subject = source.getSheets()[0].getRange('B3').getValue();
var body = source.getSheets()[0].getRange('F3').getValue();
var sheetNum = 0; // first sheet(tab) is zero, second sheet is 1, etc..
function sendEmailWithPdfAttach() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var thema = source.getSheets()[0].getRange('D3').getValue(); //
var mailTo = source.getSheets()[0].getRange('E3').getValue(); // 'D46' cell which consists an emailaddress.
var name = source.getSheets()[0].getRange('C3').getValue();
var sheets = source.getSheets();
sheets.forEach(function (s, i) {
if (i !== sheetNum) s.hideSheet();
});
var url = Drive.Files.get(source.getId())
.exportLinks['application/pdf'];
url = url + '&size=letter' + //paper size
'&portrait=false' + //orientation, false for landscape
'&fitw=true' + //fit to width, false for actual size
'&sheetnames=true&printtitle=false&pagenumbers=false' + //hide optional// was false
'&gridlines=false' + //false = hide gridlines
'&fzr=false'; //do not repeat row headers (frozen rows) on each page
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
MailApp.sendEmail(mailTo, subject, body, {
attachments: [response.getBlob().setName(name)]
});
sheets.forEach(function (s) {
s.showSheet();
})
}
答案 0 :(得分:1)
这就是我能够在互联网上找到的解决方案,在Kelvin的帮助下,我自己尝试和错误。它现在代表我工作。因为很难找到我想发布最终结果的人正在寻找这样的东西。
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Custom Menu')
.addItem('Send summary', 'menuItem1')
.addSeparator()
.addItem('Send summary&Week', 'menuItem2')
.addToUi();
}
function menuItem1() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var subject = source.getSheets()[9].getRange('G1').getValue(); //cell for subject in sheet
var body = source.getSheets()[9].getRange('F1').getValue();
var sheetNumSummary = 9; // first sheet(tab) is zero, second sheet is 1, etc..
var source = SpreadsheetApp.getActiveSpreadsheet();
var thema = source.getSheets()[9].getRange('f3').getValue(); //
var mailTo = ('youremailaddress@here.com'); //source.getSheets() [0].getRange('D1').getValue(); // 'D1' cell which consists an emailaddress.
var name = source.getSheets()[9].getRange('G1').getValue(); // Name of Attachement
var sheets = source.getSheets();
sheets.forEach(function (s, i) {
if (i !== sheetNumSummary) s.hideSheet();
});
var url = Drive.Files.get(source.getId())
.exportLinks['application/pdf'];
url = url + '&size=letter' + //paper size
'&portrait=false' + //orientation, false for landscape
'&fitw=true' + //fit to width, false for actual size
'&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional
'&gridlines=false' + //false = hide gridlines
'&fzr=false'; //do not repeat row headers (frozen rows) on each page
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
MailApp.sendEmail(mailTo, subject, body, {
attachments: [response.getBlob().setName(name)]
});
sheets.forEach(function (s) {
s.showSheet();
})
}
function menuItem2() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var subject = source.getSheets()[9].getRange('G1').getValue();
var body = source.getSheets()[9].getRange('F1').getValue();
var sheetNumSummary = 9; // first sheet(tab) is zero, second sheet is 1, etc..
var source = SpreadsheetApp.getActiveSpreadsheet();
var thema = source.getSheets()[9].getRange('f3').getValue(); //
var mailTo = ('youremailaddress@here.com'); //source.getSheets() [0].getRange('D1').getValue(); // 'D1' cell which consists an emailaddress.
var name = source.getSheets()[9].getRange('G1').getValue(); // Name of Attachement
var sheets = source.getSheets();
sheets.forEach(function (s, i) {
if (i !== sheetNumSummary) s.hideSheet();
});
var url = Drive.Files.get(source.getId())
.exportLinks['application/pdf'];
url = url + '&size=letter' + //paper size
'&portrait=false' + //orientation, false for landscape
'&fitw=true' + //fit to width, false for actual size
'&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional
'&gridlines=false' + //false = hide gridlines
'&fzr=false'; //do not repeat row headers (frozen rows) on each page
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
MailApp.sendEmail(mailTo, subject, body, {
attachments: [response.getBlob().setName(name)]
});
sheets.forEach(function (s) {
s.showSheet();
})
/*}
function menuItem2() {
*/
var source = SpreadsheetApp.getActiveSpreadsheet();
var subject = source.getSheets()[10].getRange('G1').getValue();
var body = source.getSheets()[10].getRange('F1').getValue(); //bleibt leer, kein Text notwendig
var sheetNumWeek = 10; // first sheet(tab) is zero, second sheet is 1, etc..
var source = SpreadsheetApp.getActiveSpreadsheet();
var thema = source.getSheets()[10].getRange('C1').getValue(); //
var mailTo = ('admin6142.c087880@m.evernote.com, Dwight@ndmarin.com'); //source.getSheets()[0].getRange('D1').getValue(); // 'D1' cell which consists an emailaddress.
var name = source.getSheets()[10].getRange('G1').getValue(); // Name of Attachement
var sheets = source.getSheets();
sheets.forEach(function (s, i) {
if (i !== sheetNumWeek) s.hideSheet();
});
var url = Drive.Files.get(source.getId())
.exportLinks['application/pdf'];
url = url + '&size=letter' + //paper size
'&portrait=false' + //orientation, false for landscape
'&fitw=true' + //fit to width, false for actual size
'&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional// was false
'&gridlines=false' + //false = hide gridlines
'&fzr=false'; //do not repeat row headers (frozen rows) on each page
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
MailApp.sendEmail(mailTo, subject, body, {
attachments: [response.getBlob().setName(name)]
});
sheets.forEach(function (s) {
s.showSheet();
})
}
答案 1 :(得分:0)
这是我之前在线脚本使用的东西,它应该适合你要求的一切。转到“工具”,然后转到脚本编辑器,将其粘贴,更改所需的设置并保存。重新加载表格,你会看到一个额外的选项。
//var ss = SpreadsheetApp.getActiveSpreadsheet();
//function onOpen() {
// var menu = [{name: "Email PDF", functionName: "emailSpreadsheetAsPDF"}];
// ss.addMenu("Email PDF", menu);
//};
/* Send Spreadsheet in an email as PDF, automatically */
function emailSpreadsheetAsPDF() {
// Send the PDF of the spreadsheet to this email address
var email = "kelvin@test.com
// Subject of email message
// The way it is formatted now is "Timesheet by (Get email of user who initiated the sending) and the time
var now = new Date();
var subject = "Time Sheet by " + Session.getActiveUser().getEmail() + " " + (new Date()).toString();
// Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
var ss = SpreadsheetApp.getActiveSpreadsheet();
//var body = Choose what you want as the body message
var body = "PDF generated by " + Session.getActiveUser().getEmail() + " from " + ss.getName() + " on " + (new Date()).toString();
var url = ss.getUrl();
url = url.replace(/edit$/,'');
/* Specify PDF export parameters
// From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
exportFormat = pdf / csv / xls / xlsx
gridlines = true / false
printtitle = true (1) / false (0)
size = legal / letter/ A4
fzr (repeat frozen rows) = true / false
portrait = true (1) / false (0)
fitw (fit to page width) = true (1) / false (0)
add gid if to export a particular sheet - 0, 1, 2,..
*/
var url_ext = 'export?exportFormat=pdf&format=pdf' // export as pdf
+ '&size=A4' // paper size
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true&source=kelvin' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=False' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&fitw=true' // Fit to page width
+ '&gid='; // the sheet's Id
var token = ScriptApp.getOAuthToken();
var sheets = ss.getSheets();
//make an empty array to hold your fetched blobs
var blobs = [];
for (var i=0; i<sheets.length; i++) {
// Convert individual worksheets to PDF
var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), {
headers: {
'Authorization': 'Bearer ' + token
}
});
//convert the response to a blob and store in our array
var pdfBlob = blobs[i] = response.getBlob().setName(sheets[i].getName() + '.pdf');
}
//create new blob that is a zip file containing our blob array
// var zipBlob = Utilities.zip(blobs).setName(ss.getName() + '.zip');
//optional: save the file to the root folder of Google Drive
// DriveApp.createFile(pdfBlob);
// Define the scope
// Logger.log("Storage Space used: " + DriveApp.getStorageUsed());
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {attachments:[pdfBlob]});
}