V
V
Vyacheslav902020-02-21 07:21:15
Google Apps Script
Vyacheslav90, 2020-02-21 07:21:15

How to save one sheet from Google Sheets to xlsx format and send it by email?

Good afternoon!

The task is as follows: there is a book with several sheets in it. One of the sheets must be saved in a separate book, reformatted in excel, and sent by mail.

I understand how to create a new book empty. But how to insert a sheet from another into it and convert it to xlsx, I don’t know.

If someone can help with the lines of code, I will be very grateful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2020-02-21
@oshliaer

In fact, this task is easy to google.
For example, you want to save the "My Super Sheet" sheet.

/**
 *
 */
function runSheet() {
  var spec = ['Мой супер лист'];

  var spreadsheet = SpreadsheetApp.openById(
    'ABCD1234'
  ).copy('tmp');

  spec.forEach(function(sheetName) {
    var dr = spreadsheet.getSheetByName(sheetName).getDataRange();
    dr.setValues(dr.getValues());
  });
  spreadsheet.getSheets().forEach(function(sheet) {
    if (spec.indexOf(sheet.getName()) < 0) spreadsheet.deleteSheet(sheet);
  });
  var spreadsheetId = spreadsheet.getId();
  var file = exportSpreadsheetToFile_(spreadsheetId, 'xlsx');
  DriveApp.getFileById(spreadsheetId).setTrashed(true);
  return file;
}

The listing exportSpreadsheetToFile_can be found here .
Well, you can send the received file like this
MailApp.sendEmail({
  to: "[email protected]",
  subject: "Logos",
  attachments: [runSheet().getBlob()]
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question