Insert file ID into sheet from PDF generation

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:

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.

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.

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 :wink:

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

A couple of comments on your script…

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 :slightly_smiling_face:
There are much better ways to approach this.

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

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.

1 Like

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.

1 Like