Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i changed the coding so far that i'm now able to send a spread sheet to a email address. What i'm looking for now is the possibility to select a specific range like (A1:J23) and send only that range to my mail address. any ideas what i'm doing wrong here? Always getting the warning: TypeError: Cannot find function getSheets in object Range. (line 8, file

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();
 })
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
110 views
Welcome To Ask or Share your Answers For Others

1 Answer

So thats the solution i was i able to find on the internet, with the help of Kelvin and try and error by myself. it works now on my behalf. Because it was so hard to find i wanted to post the final result for somebody who is looking for something like this.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...