Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Select google spreadsheets to send specific tabs via email

I found this code to send by email all tabs of a Google Spreadsheet as a PDF file (one merged file).

function sendReport() {
var message = {
to: "contact@sexample.com",
subject: "Sales Reports",
body: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s",
name: "Sales",
attachments:[SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("sales_report_2021")]
}
MailApp.sendEmail(message);

So, I'd like to merge into the final document only the tabs that "A1" cell is equal "1" and send them by email as attachment.

Thank you very much!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe your goal is as follows.

  • You want to create a PDF data with only the tabs of when the cell "A1" is 1.

In this case, how about the following modification?

Modified script:

function sendReport() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ranges = ss.createTextFinder("1").matchEntireCell(true).findAll();
  var showSheets = ranges.reduce((ar, e) => {
    if (e.getA1Notation() == "A1") ar.push(e.getSheet().getSheetName());
    return ar;
  }, []);
  var sheets = ss.getSheets();
  sheets.forEach(s => {
    if (!showSheets.includes(s.getSheetName())) s.hideSheet();
  });
  SpreadsheetApp.flush();
  var pdf = ss.getBlob().setName("sales_report_2021");

  var message = {
    to: "contact@sexample.com",
    subject: "Sales Reports",
    body: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s",
    name: "Sales",
    attachments: [pdf]
  }
  MailApp.sendEmail(message);

  sheets.forEach(s => s.showSheet());
}
  • In this modification, the cells which have 1 are retrieved from all sheets using TextFinder. And, the sheets that the cell "A1" has 1 are retrieved. And, the sheets that the cell "A1" has no value of 1 are hidden. And then, the Spreadsheet is converted to a PDF data, and used with MailApp.sendEmail. At last, all sheets are shown. By this flow, your goal is achieved.

References:

  • createTextFinder(findText) of Class Spreadsheet
  • hideSheet()
  • showSheet()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!