# Insert file ID into sheet from PDF generation

**URL:** <https://community.glideapps.com/t/insert-file-id-into-sheet-from-pdf-generation/25694>\
**Category:** Ask for Help\
**Created:** [April 17, 2021, 8:44pm UTC](https://community.glideapps.com/t/insert-file-id-into-sheet-from-pdf-generation/25694 "2021-04-17T20:44:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Canup](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/daniel_canup/32/32850_2.png) [@Daniel\_Canup](https://community.glideapps.com/u/Daniel_Canup)\
**Post date:** [April 17, 2021, 8:44pm UTC](https://community.glideapps.com/t/insert-file-id-into-sheet-from-pdf-generation/25694/1 "2021-04-17T20:44:33Z")

</div>

https://player.vimeo.com/video/538162026

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [April 17, 2021, 11:41pm UTC](https://community.glideapps.com/t/insert-file-id-into-sheet-from-pdf-generation/25694/2 "2021-04-17T23:41:56Z")

</div>

Firstly to only technically look at new rows in a specific Sheet, you would want to know where the event that triggers the Script is coming from.

Something like this:

> <https://stackoverflow.com/questions/49229093/google-app-script-onedit-restrict-to-sheet>

Then only continues triggering creaing a PDF when the sheet that has the change has the same name as what you specify.

Regarding the ID, I assume this will work.

> <https://stackoverflow.com/questions/42447805/how-can-i-get-the-doc-id-of-the-file-i-just-created-in-a-google-script>

Then you will edit your script to write that variable to the column that you want.

Alternatively, I assume you can do the same thing with Zapier/Integromat, looking for new file uploads for specific folders and get their ID.

---

<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 18, 2021, 4:29am UTC](https://community.glideapps.com/t/insert-file-id-into-sheet-from-pdf-generation/25694/3 "2021-04-18T04:29:56Z")

</div>

I’ve done this a lot…

To answer your first question, (only create a PDF when a new row is added). There are lots of different ways you could do this, but here is what I usually do.

- Firstly, use an onChange() trigger. In my experience, onEdit() triggers won’t be fired by Glide initiated changes.
- Next, add a column to your sheet that the trigger can check. I usually have “PDF Created At”
- When a new row is added from Glide, that column will be empty.

So the logic for the script will be something like:

- Check which sheet triggered the change, if it was your Customers sheet, then…
- Check for any rows where “PDF Created At” is empty. If you find any, then…
- Generate your PDF, and write the current time into the “PDF Created At” column

That’s the high level logic, making it robust and efficient obviously involves a little more than that 😉

For your second question (getting the PDF File ID), you’ll want something like `pdf.getId();`

A couple of comments on your script…

 ![Screen Shot 2021-04-18 at 12.04.00 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/f/3/f3864eb7737b80d76c92f687df2efdebaa8078c1.jpeg)

That’s very fragile. Hard coded column numbers will _always_ come back and bite you. The presence of the try/catch block suggests that you’ve already discovered this 🙂  
There are much better ways to approach this.

 ![Screen Shot 2021-04-18 at 12.03.35 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/c/d/cd20b84b6afa8b95ae4792699a00678a66287944.png)

If you get a bit clever about how you set up your template, then all of the above can be rewritten as:

```auto
while (headers.length > 0) {
    body.replaceText('{' + headers.shift() + '}', responses.shift());
  }

```

If you’re willing to pay, I can fix all this for you. Send me a private message.

---

<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 18, 2021, 5:01am UTC](https://community.glideapps.com/t/insert-file-id-into-sheet-from-pdf-generation/25694/4 "2021-04-18T05:01:00Z")

</div>

> [@Darren\_Murphy](#):
>
> Check for any rows where “PDF Created At” is empty. If you find any, then…

Actually, just thinking about it a bit more, that probably isn’t necessary in your case. Because you’re writing the file ID back into the sheet, you can just check that column, and only process rows where it’s empty.
