How to pull data from Google Sheets to HTML table

You can build several types of Web Apps using Google Sheets Apps Script. In previous posts, we discussed creating several types of online data entry forms using Google Apps Scripts. This post will show you how to pull data from Google Sheets to an HTML table and display it in a Web app created with Google Apps Script.

With this method, you can create beautiful tables, and you can allow visitors to search and sort data in your HTML table. This way, you can show your data to others without giving access to your Google Sheet.

I will use the following technologies to import Google Sheets data to the HTML table in our web app.

  1. Google Apps Script
  2. Bootstrap
  3. DataTables jQuery Plugin

All the above technologies are free to use. We use Bootstrap to style the Web App. The jQuery and DataTables plugin is used to add search, sort, and filter functions to the HTML table.

Pull data from Google Sheets to HTML table

Using this tutorial, you will create a web app, as shown in the following video. The data in the table is imported from a Google Sheet.

You can see the live web app from the following link.

Steps to create a web app to pull and show Google Sheets data

First, you need to prepare your Google Sheet. Then you need to create a new Apps Script project and use the code given below to create your web app. You can either modify the code to match your Google Sheet or use the Google Sheet given below.

If you do not like reading, watch the demo video below.

Step 01: Prepare your Google Sheet

First, create a simple table in your Google Sheet. Do not keep merged cells within your data range to be shown in the Web App. You can make a copy of the Google Sheets used in this example from the following link.

Step 02: Create a new Apps Script project / Make a copy

We use Google Apps Script to pull data from Google Sheets to an HTML table. There are two ways to use Google Apps Script. The first is Container-bound Scripts, and the second is Standalone Scripts. Here I am using the second, so it does not bound the script to the Google Sheet. So, you need to create the App Script Project separately, and you cannot open it using the “Tools” menu of the Google Sheet.

Below, I explain how to create a new Standalone Google Apps Script. Alternatively, you can copy the Google Apps Script project in this example with the complete code from the following link. Then you can skip this section and jump to Step 03.

To create a new Standalone Google Apps Script project,

  • Go to Google Drive
  • Click the “New” button on the top left corner and go to “More” and click “Google Apps Script“.

Then copy and paste the following code snippets.

Important !: If you are creating the project with a new Google Apps Script (instead of making a copy of the Apps Script project given above), you need to enable Google Sheets API Service for your project.

To enable Google Sheets Service,

  • Click the + icon in the Services tab,
  • Select Google Sheets API from the list in the Add a service popup box.
  • Then, click Add.

Code Snippets

Code.gs

This file includes the server-side functions.

To work the following code for your Google Sheet, you need to change the variables “spreadSheetId” and “dataRange” in lines 9 and 10 to match your Google Sheet. You can identify your Google Sheets ID as explained here.

In the above Code.gs file, the function doGet() tells the script how to serve the page and returns the HtmlOutput object. You must include this function in your script to create a web app with the HTML service.

The function getData() returns the data range as an array.

The function include(), use to include the JavaScript.html file inside the Index.html file. Using this function, I have included the codes in the JavaScript.html file in the Index.html file. You can learn more about this in HTML Service: Best Practices in Google Apps Script guide.

JavaScript.html

I have separated the client-side JavaScript codes into this JavaScript.html file. It is then included in the Index.html file using the include() function in the Code.gs file.

The following JavaScript function calls the getData() function in the Code.gs file during the page load. Then, the returned data array and passed it to the showData() function. Then it generates the data table and inserts it into the page.

Index.html

This file includes the HTML and JavaScript required to generate the HTML page. The JavaScript file mentioned above is included in the header section using include() function as shown below in line 12.

The data table is printed inside the table tag in line 19.

Step 03: Deploy as a Web App

To create the HTML page, you need to deploy your project as a Web app.

To deploy the project as a web app,

  • Click the blue color “Deploy” button
  • Click “New deployment
  • In the New deployment pop-up window,
    • For Description, give a meaningful name so that you can later identify this version.
    • Select your email for “Execute as” and then the web app will not require users to run the code using their account data.
    • Select “Anyone” for the “Who has access” section so that anyone on the internet can access this link.
  • Then click “Deploy
  • Then it will require you to authorize access to your data, click “Authorize access
  • In the next pop-up dialog box, click your email to sign in.
  • Next, click Advanced, and click the link with your project name at the bottom of the pop-up dialog box.
  • Then click the “Allow” button.
  • In the remaining pop-up dialog box, you will see a link to your Web app.
  • Click the link, and it will open the Web app in a new window. You can share this link with others.

Watch the video below that demonstrates the above steps.

YouTube player

DataTable functionalities

In this example, we used the JQuery DataTable plugin to add search, sort, pagination functions to the HTML table.

  • Search data – You can use the search box to search data in any column of the data table.
  • Sort Data – Click the table headers to sort data in ascending or descending order.
  • Pagination – By default, this table shows 10 entries per page. Click the page numbers below the table to view the other entries.
  • Change number of entries per page – Change the number of entries per page from the “Show entries” dropdown.

If your Google Sheet has a column with URLs, the above code shows them as simple text values. In order to show the URLs as hyperlinks, you can modify the JavaScript.html file as below.

Let’s say you are adding a new column with URLs to the end of the data table (column G of the Google Sheet).
Add the following code next to line 20 of the JavaScript.html file.
(Do not forget to update the dataRange in the Code.gs file according to new changes.)

{"title":"URL",
  "render": function(data, type, row, meta){
    if(type === 'display'){
      data = '<a href="' + data + '">' + data + '</a>';
    }
    return data;
  }
}

Reference: https://stackoverflow.com/a/30489991/2391195

If you want to show a custom text instead of the URL you can use the following code.

{"title":"URL",
   "render": function(data, type, row, meta){
      if(type === 'display'){
         data = '<a href="' + data + '">' + 'Your custom text' + '</a>';
       }
       return data;
    }
 }

You can see a working solution from the following links.

Wrapping Up

In this example, I showed you how to pull data from Google Sheets to an HTML table. I created the HTML table in a Web app created with Google Apps Script. Google Apps Script is completely free, and you can build many types of Web apps. I used the Bootstrap CSS library to style the HTML table. And the JQuery DataTable plugin was used to add more functionalities such as search, sort, and pagination to the HTML table.

You can share this web app with others using its URL. The visitors can get updates to the Google Sheet by refreshing the web app. Using this method, you can easily share your Google Sheets data with others without giving access to the entire spreadsheet. You can also Embed Google Apps Script Web Apps in Websites.

96 thoughts on “How to pull data from Google Sheets to HTML table”

  1. Hi. Thanks for a great tutorial. If I want to add the ability for the user to edit and delete a row do you have any pointers on what I need to google to accomplish that? Thanks.

    Reply
  2. Hello, in this tutorial, it is only possible to show one tab at a time, correct? If I want to show a table with several tabs, how can I do it?

    Reply
  3. Thanks for your explanation. I am getting this error –
    DataTables warning: table id=data-table – Requested unknown parameter ‘7’ for row 0, column 7. For more information about this error, please see http://datatables.net/tn/4

    I tried exactly how you told but above error is coming if you can provide any solution above error I will be thankfull to you

    Reply
    • I had this same issue today. In my case it was due to empty cells in my spreadsheet. Since it was a numeric column I was able to populate the empty cells with ‘0’ and this eliminated the error.

      Reply
  4. I followed your guide and change the three parameters as explained which “sheet ID” “data range” & “Sheet name”. But after deployment it shows blank web page. I could not locate the error caused. Also do we need to replace the filename with any Code.gs 18 to 22
    //INCLUDE JAVASCRIPT AND CSS FILES
    function include(filename) {
    return HtmlService.createHtmlOutputFromFile(filename)
    .getContent();
    }
    Thank you

    Reply
  5. Code sample effected and publish on google sites page.
    One table column contains URL’s with links to other google site and external pages.
    The links do work do not work within google site page (iframe issue??)
    Thanks for any help

    Reply
  6. Hi admin, i did all the code you said above resultantly my html page shows nothing, an empty page, can you or someone help me in this regard, please.

    Reply
  7. Hi, Thank you for the info, was really useful.
    is there a way to add 3 tables from 3 different sheets?
    Thank you in advance 🙂

    Reply
  8. Hi. Thanks for a great tutorial. If I want to add the ability for the user to edit and delete a row do you have any pointers on what I need to google to accomplish that? Thanks.

    Reply
  9. function getData() {
    var spreadSheetId = “1FBcJ-eLeL9mFNOUrI8zpveiFNSHwdwSdjzkVGd5Dc1Y”; //CHANGE
    var dataRange = “Data!A2:F”; //CHANGE

    var range = Sheets.Spreadsheets.Values.get(spreadSheetId, dataRange);
    var values = range.values;

    return values;
    ======================================
    20.46.01 Error
    ReferenceError: Sheets is not defined
    getData @ Code.gs:10

    Sheets is not defined why

    Reply
  10. Great tutorial!👏
    I have some questions:
    – Is it possible to hide results if search dialog is empty, or to put a default text like ‘put your text here’?
    – How do I limit the search for looking up in one column instead of all? (i.e. only search by name, and not by other column)
    – How do I change default language for ‘next’, ‘previous’, etc ?
    – Is it possible to limit search results for showing only two newest entries?

    Thank you!

    Reply
  11. can i move it to my nextjs app? or it can only use in app script?
    and how do i change the page indicator’s color from blue to other color?

    Reply
  12. Hi.

    I have used your template to my table that have 5 columns.
    I am trying to put result on google script html on all screen but it stays always centered, with some margins on both sides.

    Can you tell me how can i configure columns setup so table fill all screen (horizontal).

    Thanks

    Reply
    • Hi,
      Try replacing lines 3,4,5 (doGet function) with the following,

      function doGet() {
      var html = HtmlService.createTemplateFromFile(‘Index’).evaluate();
      return html.addMetaTag(‘viewport’, ‘width=device-width, initial-scale=1’);
      }

      Reply
      • Hi.

        Thank you for your support.

        I tryed but it didn’t work.

        I had to put ” instead of ‘ because gave an error.

      • Hi.

        I had a issue with the ‘, giving error like i wrote on last message,

        I fix it and iused you code but it dind’t work.

  13. Hi!!
    I have copied the script and followed the youtube guide. Changing the ID of the SpreadSheetID and the dataRange with the name of the sheet and the range. Also the names of the columns.
    When I deploy, I give it permissions and it loads the page but blank. And I am not able to locate where the error is because you only have to change 3 data and add the sheets service.

    Could you tell me why I get a blank page?

    Thank you very much

    Reply
  14. Hi
    I keep getting “This application was created by another user, not by Google.”
    Any ideas where I am going wrong?
    Much appreciated
    Christopher

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: