Open In App

How to Share Specific Fields Of Google Form With Google Scripts?

Last Updated : 15 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Google Forms are one of the most used forms for conducting survey but sometimes the organizer of the survey want to share live results of the Google forms with the audience this can be done easily using Google scripts. As we all know that the data entered by the user in Google form is stored directly in a Google sheet. So we will use gs(Google script)  a programming language provided by Google to do much more with their products.  

To do so follow the below steps:

Step 1: Create a Google form and open the Google sheet associated with it, you can find the Google sheet associated with your Google form on the Google sheets home page or you can create another spreadsheet for your Google forms. To do so

  • Click on response.

  • Now click on the spreadsheet button.

  •  Finally, click on create new spreadsheet as shown below:

Step 2: Now open the Google sheets 

  • First click on Tools.

  • Now click on Script editor.

Step 3: After Clicking On scripts a new window will open with a default file name code.gs. Here clear all the content:

Step 4: After that paste the below code in code.gs and run the extract function. After executing the extract function a pop up will be raised to review permissions, click on review permissions after that select your Google account through which form is created, and then click on advanced  go to your project name and then allow the script to read google sheets. After that click again on the run. 

 function extract(){
  var tmp = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
  var html = "";
  html += "<table>"
  for(var i=0;i<tmp.length;i++){
    html += "<tr>"
    for(var z=0;z<tmp[i].length;z++){
      html += "<th>"
      html += tmp[i][z];
      html += "<th>"
    }
    html += "</tr>"
  }
  html += "</table>";
  Logger.log(html);
  return html
}

To check if the script executed properly view logs. To do so:

  • Go to views:

  • Now click on logs:

If you see your spreadsheet in HTML format inside your log then the extract function is working fine.

Step 5: Now the final procedure is to create a doGet function and deploy the website. Now paste the doGet function in the code.gs file along with the extract function

function extract(){
  var tmp = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
  var html = "";
  html += "<table>"
  for(var i=0;i<tmp.length;i++){
    html += "<tr>"
    for(var z=0;z<tmp[i].length;z++){
      html += "<th>"
      html += tmp[i][z];
      html += "<th>"
    }
    html += "</tr>"
  }
  html += "</table>";
  Logger.log(html);
  return html
}
function doGet(e){
  return HtmlService.createHtmlOutput(extract());
}

After writing the complete code:

  •  Go on publish

  • Now deploy as web app. A URL of the app will be provided to you.

Step 6(Optional). Now suppose if you don’t want to show all the columns then modify the code for columns you want to show for example in this case we will show only “should we publish this article”

function extract(){
  var tmp = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
  var html = "";
  html += "<table>"
  for(var i=0;i<tmp.length;i++){
    html += "<tr>"
      html += "<th>"
      html += tmp[i][3];
      html += "<th>"
    html += "</tr>"
  }
  html += "</table>";
  Logger.log(html);
  return html
}
function doGet(e){
  return HtmlService.createHtmlOutput(extract());
}

You can do much more with this. You can show only count of users who said yes or no, you can add your custom HTML and can also add some CSS to it. 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads