Plug Sample #5 - Read and write values/data in a Google sheet with Zobot

Plug Sample #5 - Read and write values/data in a Google sheet with Zobot

We all know google sheets are one of the widely used spreadsheet software. It helps us to make data-driven decisions. When connecting the Zobot with sheets, the bot can display values/data from a spreadsheet, like the available stock details (Item price, quantity). Create a new record, such as feedback, comments or even update the existing fields based on your requirement. In this post, we will focus on using plugs to achieve the below google sheets actions with REST API. 
  • Fetching records from Google sheets
  • Creating records in Google sheets
As we're aware, plugs in the Codeless bot builder are used to create custom actions or third-party integrations using deluge. You can create a plug and add it as a block in the bot builder, like rest blocks. Now let's learn how to create this plug step by step. 
Step 1 - Creating a connection with Google service.   
  • In your SalesIQ Dashboard, navigate to Settings > Developers > Plugs > Click on Add.
  • Provide your plug a name, and description, select the Platform as SalesIQ Scripts, and finally, click on Connection to your left bottom. You will be redirected to the connection interface. 

  •  Click on Create connection at the top right corner. Under Default connection, select Google service. 

  • Click on Create And Connect. Upon successful authentication,  Zoho SalesIQ will be connected with the Google service. 
Note: The Connection Link Name is used to scripts to invoke URL tasks.

Step 2 - Create the plug using Connection:
First let's look at fetching records from Google sheets. Any data that is available in sheets can be fetched. For example, if you're running an e-commerce business and maintaining all the stock information such as item name, quantity, price etc. on sheet, those details can be fetched here and displayed to the visitor. 
  • Copy, paste the below code and changes the sheet ID, name, row range, and connection name. 
  1. //Add a output parameter named "allproducts" and select "Option list" as datatype
  2. google_spreadsheet_id = "1y92rI_ITWLxpXOQMtyO8W9_7P6HwygxfneeVxdoRd-E";
  3. sheet_name = "Fetching+records";
  4. //specify the correct range like "a1:b11"
  5. url = "" + google_spreadsheet_id + "/values/" + sheet_name + "!a2:b11?majorDimension=ROWS";
  6. //googlesheets - Connection link name made using the connection interface (Step 1)
  7. apiresponse = invokeurl
  8. [
  9. url :url
  10. type :GET
  11. connection:"googlesheets"
  12. ];
  13. values = apiresponse.get("values");
  14. productList = List();
  15. //Adding all the product and ID in an option list | Ref -
  16. for each  entry in values
  17. {
  18. id = entry.get("0");
  19. product = entry.get("1");
  20. productList.add({"id":id,"text":product});
  21. info productList;
  22. }
  23. response = Map();
  24. //parasing the plug output 
  25. // allproducts - Name of output parameter; productList - Value to be associated to "allproducts"
  26. response.put("allproducts",productList);
  27. return response;
  • Then, click save, preview the plug and publish it. For your reference, we have made the sheet public.
Note: API invoked in the plug spreadsheets.values.get API

  • The above reference shows the spreadsheet ID and the sheet name.
Now, let's look at creating records in Google sheets. Any type of data can be pushed to sheets, such as feedback, comments, etc. 
  •  Copy, paste the below code and change the sheet ID, name, and connection name.
  1. //Add two input parameters named "visitorID","feedback" and select the datatype as "String"
  2. if(session.containsKey("visitorID"))
  3. {
  4. id = session.get("visitorID").get("value");
  5. }
  6. if(session.containsKey("feedback"))
  7. {
  8. feedback = session.get("feedback").get("value");
  9. }
  10. google_spreadsheet_id = "1y92rI_ITWLxpXOQMtyO8W9_7P6HwygxfneeVxdoRd-E";
  11. sheet_name = "Create+records";
  12. //specify the correct range like "A3:B3"
  13. url = "" + google_spreadsheet_id + "/values/" + sheet_name + "!A3:B3:append?valueInputOption=USER_ENTERED";
  14. params = {"majorDimension":"ROWS","values":{{id,feedback}}};
  15. apiresponse = invokeurl
  16. [
  17. url :url
  18. type :post
  19. parameters:params.toString()
  20. headers:{"Content-Type":"application/json"}
  21. connection:"googlesheets"
  22. ];
  23. info apiresponse;
  24. response = Map();
  25. return response;
  • Then, click Save, preview the plug and Publish it. For your reference, we have made the sheet public
Note: API invoked in the plug spreadsheets.values.append
  •  Navigate to Settings > Bot > Add, provide the necessary information, and select Codeless Bot Builder as a bot platform or open an existing bot.
  •  Select the Plugs under Action Block and select the required plug (Only published plugs will be listed here)
  •  Provide the plug inputs and outputs.
  •  Click Save

In the above video, we've used a plug, fetched some products and displayed them as options to the visitor. 
Heads up:
Related links:
To know more about the features of Zobot, kindly visit our Resources Section. 

Sasidar Thandapani