# Help in App Script (Convert to PDF and Email straightway)

**URL:** <https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573>\
**Category:** Ask for Help\
**Created:** [April 15, 2021, 1:40am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573 "2021-04-15T01:40:29Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![biha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/biha/32/84110_2.png) [@biha](https://community.glideapps.com/u/biha)\
**Post date:** [April 15, 2021, 1:40am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/1 "2021-04-15T01:40:29Z")

</div>

Hi Glider,

I’m using Glide to have data in a few sheets.  
I create a template using google sheet itself convert to PDF and straight away email the user using this script.

> var changedFlag = false;  
> var TEMPLATESHEET=‘Boom-Report’;
> 
> function emailSpreadsheetAsPDF() {  
> DocumentApp.getActiveDocument();  
> DriveApp.getFiles();
> 
> // This is the link to my spreadsheet with the Form responses and the Invoice Template sheets  
> // Add the link to your spreadsheet here  
> // or you can just replace the text in the link between “d/” and “/edit”  
> // In my case is the text: 17I8-QDce0Nug7amrZeYTB3IYbGCGxvUj-XMt8uUUyvI  
> const ss = SpreadsheetApp.openByUrl(“[Service Apps - Google Sheets](https://docs.google.com/spreadsheets/d/1NVJOdFLBAgNFqSHhnHJYybjUlSqhv4hKI_HXJyhJ88E/edit)”);
> 
> // We are going to get the email address from the cell “B7” from the “Invoice” sheet  
> // Change the reference of the cell or the name of the sheet if it is different  
> const value = ss.getSheetByName(“Source Email-Boom”).getRange(“X3”).getValue();  
> const email = value.toString();
> 
> // Subject of the email message  
> const subject = ss.getSheetByName(“Source Email-Boom”).getRange(“B3”).getValue();
> 
> ```
> // Email Text. You can add HTML code here - see ctrlq.org/html-mail
> 
> ```
> 
> const body = “Boom Lifts Inspection Report - Sent via Auto Generate PDI Report from Glideapps”;
> 
> // Again, the URL to your spreadsheet but now with “/export” at the end  
> // Change it to the link of your spreadsheet, but leave the “/export”  
> const url = ‘[https://docs.google.com/spreadsheets/d/1NVJOdFLBAgNFqSHhnHJYybjUlSqhv4hKI\_HXJyhJ88E/export?](https://docs.google.com/spreadsheets/d/1NVJOdFLBAgNFqSHhnHJYybjUlSqhv4hKI_HXJyhJ88E/export?)’;
> 
> const exportOptions =  
> ‘exportFormat=pdf&format=pdf’ + // export as pdf  
> ‘&size=A4’ + // paper size letter / You can use A4 or legal  
> ‘&portrait=true’ + // orientation portal, use false for landscape  
> ‘&fitw=true’ + // fit to page width false, to get the 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  
> ‘&gid=671631174’; // the sheet’s Id. Change it to your sheet ID.  
> // You can find the sheet ID in the link bar.  
> // Select the sheet that you want to print and check the link,  
> // the gid number of the sheet is on the end of your link.
> 
> var params = {method:“GET”,headers:{“authorization”:"Bearer "+ ScriptApp.getOAuthToken()}};
> 
> // Generate the PDF file  
> var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
> 
> // Send the PDF file as an attachement  
> GmailApp.sendEmail(email, subject, body, {  
> htmlBody: body,  
> attachments: [{  
> fileName: ss.getSheetByName(“Source Email-Boom”).getRange(“B3”).getValue().toString() +“.pdf”,  
> content: response.getBytes(),  
> mimeType: “application/pdf”  
> }]  
> });
> 
> // Save the PDF to Drive. The name of the PDF is going to be the name of the Company (cell B5)  
> const nameFile = ss.getSheetByName(“Source Email-Boom”).getRange(“B3”).getValue().toString() +“.pdf”  
> DriveApp.createFile(response.setName(nameFile));  
> }
> 
> function onChange(e) {
> 
> var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();  
> var SheetName = ActiveSheet.getSheetName();  
> Logger.log (“ON0”);  
> Logger.log (“SHEET:” + SheetName);  
> if (SheetName===TEMPLATESHEET) {  
> Logger.log (“ON1”);  
> //the below will prevent onChange function to run multiple times  
> if(changedFlag == true) {  
> changedFlag = false;  
> return;  
> }  
> //only run functions if changedFlag is False  
> if(e.changeType === ‘EDIT’) {  
> Logger.log (“ON2”);  
> emailSpreadsheetAsPDF() ;  
> changedFlag = true;  
> }  
> }  
> }

My problem is, I need to be triggered when only on **one** sheet changes.  
How can i do that?  
Currently, they triggered when there’s a changes in the whole workbook.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [April 15, 2021, 1:46am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/2 "2021-04-15T01:46:19Z")

</div>

> [@biha](#):
>
> My problem is, I need to be triggered when only on **one** sheet changes.  
> How can i do that?

Use a wrapper function as your onChange() trigger.  
That function should call `e.source.getActiveSheet().getName();` to find out which sheet triggered the change.  
If it’s the sheet you care about, run the rest of your script/s.  
If it isn’t, do nothing.

Here is a simple example:

```auto
function on_sheet_change(event) {
  var sheetname = event.source.getActiveSheet().getName();
  var sheet = event.source.getActiveSheet();
  
  if (sheetname == 'moo') {
    do_something_with(sheet);
  }
}

```

---

<div class="post-metadata">

**Author:** ![biha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/biha/32/84110_2.png) [@biha](https://community.glideapps.com/u/biha)\
**Post date:** [April 15, 2021, 1:52am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/3 "2021-04-15T01:52:39Z")

</div>

Thanks for your prompt reply Darren,

But should remove my current OnChange function?

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [April 15, 2021, 1:55am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/4 "2021-04-15T01:55:44Z")

</div>

Just edit it and point it at your wrapper function.

---

<div class="post-metadata">

**Author:** ![biha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/biha/32/84110_2.png) [@biha](https://community.glideapps.com/u/biha)\
**Post date:** [April 15, 2021, 2:00am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/5 "2021-04-15T02:00:14Z")

</div>

It is something like this ?

> function onChange(e) {
> 
> var sheetname = event.source.getActiveSheet().getName();
> 
> var sheet = event.source.getActiveSheet();
> 
> Logger.log (“ON0”);
> 
> Logger.log (“SHEET:” + SheetName);
> 
> if (sheetname == ‘Boom-Report’) {
> 
> ```
> Logger.log ("ON1");
> 
> //the below will prevent onChange function to run multiple times
> 
> if(changedFlag == true) {
> 
> changedFlag = false;
> 
> return;
> 
> }
> 
> //only run functions if changedFlag is False
> 
> if(e.changeType === 'EDIT') {
> 
> Logger.log ("ON2");
> 
> emailSpreadsheetAsPDF() ;
> 
> changedFlag = true;
> 
> }
> 
> ```
> 
> }
> 
> }

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [April 15, 2021, 2:06am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/6 "2021-04-15T02:06:24Z")

</div>

i would also check if active cell value is not empty to avoid triggering when deleting a row

---

<div class="post-metadata">

**Author:** ![biha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/biha/32/84110_2.png) [@biha](https://community.glideapps.com/u/biha)\
**Post date:** [April 15, 2021, 2:12am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/7 "2021-04-15T02:12:08Z")

</div>

Thanks Uzo for replying ,  
But what do you mean?  
I’m taking this script from google.  
I understand how its work, but cant really know exactly what each of script means.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [April 15, 2021, 2:16am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/8 "2021-04-15T02:16:57Z")

</div>

No, that won’t work.

1. You defined the function using `e`, but then you try to reference `event`. So that will cause the function to immediately fail.
2. Your use of `changedFlag` will cause `emailSpreadsheetAsPDF()` to be called once, and then it will _NEVER_ be called again.

Try something like this:

```auto
function onChange(event) {
  var sheetname = event.source.getActiveSheet().getName();
  if (sheetname == 'Boom - Report') {
    if (event.changeType === 'EDIT') {
      emailSpreadsheetAsPDF();
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [April 15, 2021, 2:17am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/9 "2021-04-15T02:17:05Z")

</div>

when deleting a row, in that sheet trigger on change will fire… so you can add to the script activeCell != “”  
so it will ignore empty change

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [April 15, 2021, 2:23am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/10 "2021-04-15T02:23:12Z")

</div>

> [@Darren\_Murphy](#):
>
> Your use of `changedFlag` will cause `emailSpreadsheetAsPDF()` to be called once, and then it will _NEVER_ be called again.

Actually, you didn’t even define `changeFlag`, so your script will fail as soon as it hits the line that tries to check that.

---

<div class="post-metadata">

**Author:** ![biha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/biha/32/84110_2.png) [@biha](https://community.glideapps.com/u/biha)\
**Post date:** [April 15, 2021, 2:24am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/11 "2021-04-15T02:24:08Z")

</div>

> [@Darren\_Murphy](#):
>
> ` var sheet = event.source.getActiveSheet();`

This is no need?

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [April 15, 2021, 2:27am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/12 "2021-04-15T02:27:45Z")

</div>

It depends how you plan to call the second function.  
I generally use it, and then call the second function passing the `sheet` object.  
This saves me having to do something like:

```auto
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

```

in my called function.  
If your called function already does that, then it isn’t necessary.

---

<div class="post-metadata">

**Author:** ![biha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/biha/32/84110_2.png) [@biha](https://community.glideapps.com/u/biha)\
**Post date:** [April 15, 2021, 2:33am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/13 "2021-04-15T02:33:04Z")

</div>

Hmmm okay. Not really clear. But i’ll try to put both. 🙂

---

<div class="post-metadata">

**Author:** ![biha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/biha/32/84110_2.png) [@biha](https://community.glideapps.com/u/biha)\
**Post date:** [April 15, 2021, 5:14am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/14 "2021-04-15T05:14:40Z")

</div>

> [@biha](#):
>
> // Save the PDF to Drive. The name of the PDF is going to be the name of the Company (cell B5)  
> const nameFile = ss.getSheetByName(“Source Email-Boom”).getRange(“B3”).getValue().toString() +“.pdf”  
> DriveApp.createFile(response.setName(nameFile));  
> }

Hi again,

Regarding to the above script, do you know how to save in a folder in Gdrive.  
Currently, it saved not in any folder. So , it get quite mess.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [April 15, 2021, 5:22am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/15 "2021-04-15T05:22:23Z")

</div>

I have an example somewhere. I’ll dig it out and post later.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [April 16, 2021, 2:02am UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/16 "2021-04-16T02:02:32Z")

</div>

> [@biha](#):
>
> Regarding to the above script, do you know how to save in a folder in Gdrive.

You just need to identify the folder ID, then you can move the file to that folder.  
I have code that does this, but I don’t think sharing it with you will help much, as it’s for very specific use cases. I don’t have any code that you can just copy/paste and use.

My advice is to read up on the required methods, gain an understanding of how they work, and write the code yourself.

- [DriveApp.getFolderById()](https://developers.google.com/apps-script/reference/drive/drive-app?hl=en#getfolderbyidid)
- [file.moveTo()](https://developers.google.com/apps-script/reference/drive/file?hl=en#movetodestination)

---

<div class="post-metadata">

**Author:** ![NoCodeAndy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/nocodeandy/32/62530_2.png) [@NoCodeAndy](https://community.glideapps.com/u/NoCodeAndy)\
**Post date:** [January 17, 2024, 4:52pm UTC](https://community.glideapps.com/t/help-in-app-script-convert-to-pdf-and-email-straightway/25573/17 "2024-01-17T16:52:42Z")

</div>


