0

I want to send out only one tab of a specific sheet by Outlook mail using Google App script in Google Spreadsheet.

I searched and wrote the script, but it is too old to work or an error occurs.

What i want Among the multiple tabs of the List sheet of the spreadsheet, the data from sheet 33 to column A1:M are saved as "values" in .xls and attached to Outlook mail and sent

And how do I add a day to today's date in new Date()?

If there is an expert who can help me, it would be a great help.

Thanks.

function sendEmail(){

        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet=ss.getSheetByName("Sheet33");
        var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + ss.getSheetName() + "&exportFormat=xlsx";

        var d = Utilities.formatDate(new Date(), "GMT+9", "yyyyMMdd");
             

    var token         = ScriptApp.getOAuthToken();
    
    var response      = UrlFetchApp.fetch(url, {
      headers: {
        'Authorization': 'Bearer ' +  token
    
    },
          muteHttpExceptions: true
      
    });

  var blobs   = [response.getBlob().setName(ss.getSheetName() + ".xlsx")];

  var receipient = "khan@abc.com"
  var subject = "Date_"+ d + ss.getSheetName() +" List share."
  var emailbody = "Date_"+ d + ss.getSheetName() +" List share.\nThanks."

  MailApp.sendEmail(receipient, subject, emailbody, {attachments: blobs});

  Browser.msgBox("Date_"+ d + ss.getSheetName() + " Send Mail Complete.", Browser.Buttons.OK);
  }```
Khan HB
  • 11
  • 6
  • Hi! I posted an answer. I didn't add the detail of exporting a specific range in the sheet, and not the entire sheet, but I mentioned how this can be done. Let me know if you need more assistance on this. – Iamblichus May 03 '21 at 09:15

1 Answers1

0

Issue:

You are using an old export URL (https://docs.google.com/feeds/...), I don't think this can be used anymore. Also, you are providing the spreadsheet name instead of the id.

Solution:

The export URL should be something like this instead:

var url = "https://docs.google.com/spreadsheets/d/" + spreadsheetId + "/export?format=xlsx&gid=" + sheetId;

Code snippet:

function sendEmail(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = ss.getId();
  var sheet = ss.getSheetByName("Sheet33");
  var range = sheet.getRange("A1:M");
  var tempSheet = ss.insertSheet("My temporary Sheet");
  range.copyTo(tempSheet.getRange("A1:M"), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  var tempSheetId = tempSheet.getSheetId();
  var url = "https://docs.google.com/spreadsheets/d/" + spreadsheetId + "/export?format=xlsx&gid=" + tempSheetId;
  var tomorrow = new Date();
  tomorrow.setDate(new Date().getDate() + 1);
  var d = Utilities.formatDate(tomorrow, "GMT+9", "yyyyMMdd");             
  var token = ScriptApp.getOAuthToken();    
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' +  token  
    },
    muteHttpExceptions: true   
  });
  var blobs = [response.getBlob()];
  var receipient = "khan@abc.com";
  var spreadsheetName = ss.getName();
  var subject = "Date_"+ d + spreadsheetName +" List share."
  var emailbody = "Date_"+ d + spreadsheetName +" List share.\nThanks."
  MailApp.sendEmail(receipient, subject, emailbody, {attachments: blobs});
  ss.deleteSheet(tempSheet);
  Browser.msgBox("Date_"+ d + spreadsheetName + " Send Mail Complete.", Browser.Buttons.OK);
}

Notes:

  • In order to add a day to your date, you can do something like this. That's what I did in the sample above.
  • In this sample, the entire Sheet33 is exported. If you want to export a specific range instead, you should (1) copy the desired range to a new temporary sheet, (2) export that sheet, (3) delete the sheet. See, for example, this answer.
Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • Thank you very much! Thanks to Iamblichus I finally solved it! With the help provided, only one tab you want is converted and sent as an attachment to an e-mail. ``` var startRow = 1; var numRows = sheet.getLastRow(); var dataRange = sheet.getRange (startRow, 1, numRows, 13) ``` I have created a script that contains scoping. This script doesn't work yet, but I'm going to try again. Also, I want to convert the formula to text and attach it as a value. Your comments have been very helpful to me! Read about the optional scripts you added. I will use it later! Thank you again. – Khan HB May 04 '21 at 03:23
  • 너무 기쁜 나머지 잊고 있었네요. 혹시 제가 추가한 질문에 대한 답변도 가능할까요? 제안해 주신 질문에 대한 내용을 모두 읽어보고 나의 스크립트에 적용해보고자 하였으나 작동하지 않습니다. https://stackoverflow.com/questions/61394978/exporting-specific-range-of-cells-to-pdf-google-sheets/61403951#61403951 https://docs.google.com/spreadsheets/d/{spreadsheet_id}/export?exportFormat=pdf&format=pdf&gridlines=false&size=A4&gid={gid_id}&range=b2:e46 – Khan HB May 04 '21 at 08:02
  • 1
    @KhanHB You should copy the range to a new sheet and export that sheet, did you do that? Later I'll update my answer explaining this a bit more. – Iamblichus May 04 '21 at 08:09
  • Oh I didn't. According to the answer linked to the link ``` gid = sheetId & range = NamedRange ``` I tried setting the range by format, but it didn't succeed. If there is a new update, it will be of great help. Thank you:) – Khan HB May 04 '21 at 08:12
  • If the formula content of sheet33 is How do I convert .xlsx by changing it to a value? I'm asking a question because it fails even when I use Utilities – Khan HB May 04 '21 at 08:20
  • @KhanHB I updated my script so that (1) it creates a new temporary sheet, (2) copy the source range to the temporary sheet (only values, not formulas, as I think that's what you want), (3) export the temporary sheet and (4) delete the temporary sheet. I hope this is useful to you. – Iamblichus May 04 '21 at 10:58