Creating a table in Google Doc from an Array of information

Anyone got a tutorial on how to take an array of items and turn them into or add them to a table in a google doc? Like five rows, 4 columns. I’m kind of stuck…

What I have right now is a purchase order template inside a google doc. When someone places an order on my site certain fields are populated on that template. So far everything works great.

What I am missing is that the order may contain multiple products that were added to the cart and I need to take those items into a table in the template. For example one person may order 3 items and another may order 6 items. So I need to dynamically insert each line into the table on the template.

The table has a Product Name, Quantity, Price, and Subtotal.

Right now I send all of the items ordered into one cell and then manually fix the new purchase order before I finalize it.

Hi John,
I don’t have a complete tutorial, but have you tried using “Google Docs → Make an API call”? So, for example to create a table of five rows and four columns you would call POST /v1/documents/{{documentID}}:batchUpdate with a payload:

{
  "requests": [
    {
      "insertTable": {
        "rows": 5,
        "columns": 4,
        "location": {
          "segmentId": "",
          "index": 1
        }
      }
    }
  ]
}

This adds a new empty table to the documents body.

2 Likes

@zezutom Thanks for that idea, I’ll look into it. It may get me closer. I’ll update my post with a little more detail to try to better explain what I want to achieve.

1 Like

Reading your updated description, now it makes a perfect sense! I think you are close to have it solved. If I were you, I would add Google Docs → Make an API call as a follow-up action for Google Docs -> Create a Document from a Template. For each of the purchased items, you want to make an InsertTableRowRequest in the same fashion as I shared in my previous comment (the payload would be of course different).

2 Likes

If you know HTML, you can place a set variable between your iterator and text aggregator and set it up so that it does the HTML for each row.

The only catch is you won’t be able to use the Create a Document from a Template, you’ll have to use the Create the Document module instead.

1 Like

Hey @zezutom have you been successful in getting the Make and Api call work with a Google doc module? I’m stuck on this… I was trying to test out your idea but have had no luck today.

hi @johnematias ,
I have indeed. See the attached blueprint, hope you find it useful.
blueprint.json (8.9 KB)

In a nutshell, it picks the first doc in a root folder on my (test) google drive, stores the document id and makes an api call appending an empty table using the document id.

3 Likes

@zezutom You are awesome! Thanks so much for sharing that. I was able to get the blueprint to work in a test the way you have it set up in that example. And I figured out my error by looking at how you had the make an api module set up.

I’ll be working to see if I can configure the insert table row request today. Fingers crossed! :slight_smile:

2 Likes

You are welcome, glad I could help.

2 Likes

Hi there,

first post.
I hope this community becomes the No.1 reference for integromat/make (once it will show up in google search results).

I had the same issue and came across several approaches:

In implementing the 4th approach, there were still a few pitfalls/learnings down the road, which I want to share:

  1. iterate through the reserve(body.content) array
  2. filter if “table” exists
  3. reserve bundle order by setting a variable to: “total number of bundles” +1-“bundle order position”
  4. (optional) get a reference from the first cell of a table: {{9.body.content[30.reversed order].table.tableRows[1].tableCells[1].content[1].paragraph.elements[1].textRun.content}}
  5. search rows in google sheets whose content you want to insert in google docs
  6. aggregate the desired content and the tables’ start index, end index and reversed order to an array
  7. iterate array
  8. make a POST request to this URL: /v1/documents/{{9.documentId}}:batchUpdate with the following body:
    {
    requests: [
    {
    insertTableRow: {
    tableCellLocation: {
    tableStartLocation: {
    index: {{9.body.content[43.reversed order].startIndex}}
    },
    rowIndex: 1,
    columnIndex: 0
    },
    insertBelow: false
    }
    },
    {
    insertText: {
    location: {
    index: {{(9.body.content[43.reversed order].endIndex - 2)}}
    },
    text: “XY”
    }
    }
    ]
    }
3 Likes

Thanks @Gregory the reverse order is a smart move! Could you share the blueprint of your scenario? I’m struggling to rebuild it.

I’m still stuck how to insert content via the API. @johnematias any advice or blueprints to look would be appreciated