Section | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
...
- It’s triggered by clicking on the Update Google Sheet button.
- We setup headers and an update query using your access token and spreadsheet key (the long ID in the URL of the Google Sheet). In this example, the name of the Google Sheet tab is Locations. Change the wsname= parameter to the name of your Google Sheet tab if you named the tab something different.
- On Premise customers need to also specify the server and port where their Google Connector is hosted. The updatequery url will start with '<YourServer:Port>/google/spreadsheets/'.
- Add var params to identify the column names and control values that will update them.
- Add var updateparams to convert the params to a string. Notice that this method uses the parameter '&updatesjson=' to exchange data to/from the Google sheet. When sending data in this manner, the data has to be a string, so we also use the function JSON.stringify(<json variable>) to convert it.
- Run the update – perform an http.put() and eval the results.
Tip |
---|
The column name on a Google sheet must match the control name. The matching is case-insensitive and any spaces in the column name are ignored. A control named "FirstName" matches a column header "first name." However, references to Google Sheet columns in your rule must be lower case and cannot contain spaces. The correct reference for this example is "firstname." |
Note |
---|
Reserved characters in a URL need to be encoded. The example above uses the encodeURIComponent function in JavaScript for every parameter value and invoke the http function with the extra encode parameter set to false. |
...
- Add a Table Control to your form/workflow. In this example Contact Information form, our table has four columns: lineNum (Text), Contact Type (Dropdown), Phone (Phone), Email (Email).
- Set up your Google Sheet with column names that match the control names in your form. You'll need two additional elements:
- The first column in your submissions Google Sheet must be the line number. Prepopulate this with line numbers. In our example this tab is named "ContactInfo".
- Add another tab in your sheet named "getline". You can name the columns whatever you want. The first column will be used as a read query, and the second column (cell B2) will represent the last available row in your sheet. Set it to "1" for now. Your business rule will update it.
- The first column in your submissions Google Sheet must be the line number. Prepopulate this with line numbers. In our example this tab is named "ContactInfo".
- Add two business rules. Replace the info shown in <> in these examples with your Google Sheet details (e.g. replace <spreadsheet key> with your spreadsheet's key, found in the spreadsheet URL.)
Get Line Numbers:
Code Block /*member nextln, password, user, results */ var event = form.load; var event2 = infoTableRepeat.itemAdded; //name of your table or repeat control var x; var id; var idm; if (form.load){ var headers = {"user":"<google username>","password":"<OAuth Token>" }; for (var i = 0; i < lineNum.value.length; i++){ //lineNum is a control in your table/repeat //query to read from the extra tab in your sheet that you named 'linegetline'. The query is A1="A2". var readquery = '/google/spreadsheets/query/key/<spreadsheet key>?wsname=linegetline&query=<cell A1>="<cell A2>"'; //query to update that same tab. var updatequery = '/google/spreadsheets/update/key/<spreadsheet key>?wsname=linegetline&query=<cell A1>="<cell A2>"'; eval('x=' + http.get(readquery, headers)); //sets the line numbers in the table id = x.results[0].nextln; //"nextln" is the ColB header (cell B1 in your tab) if (lineNum[i].value.length === 0){ lineNum[i].value = id; id = id * 1 + 1; } if (id !== 'unknown') { eval('x=' + http.put(updatequery + '&updates=nextln=' + id, null, headers, false)); //update nextln with the next available line number (# of rows in your table + 1) } } }
Update Rows from Table
Code Block /*member password, user, ContactType, Email, Phone, firstname, lastname, linenumber*/ if (trigger.clicked){ //Variables for controls that are NOT in the repeat var fname = firstName.value; var lname = lastName.value; //Login var headers = {"user":"<google username>","password":"<OAuth Token>" }; for (var i = 0; i < ContactType.value.length; i++){ //ContactType is a column in your table. This loop repeats for each table row that has a value in this column. var ln = lineNum[i].value; //Variable that gets the line number set by your Get Line Number rule, corresponds to an available row in the Google Sheet //query to update the tab "ContactInfo" when the first column "line" equals the line number (ln) in your table var updatequery = '/google/spreadsheets/update/key/<your spreadsheet key>?wsname=ContactInfo&query='+encodeURIComponent('line="'+ ln + '"'); //variables for controls that ARE in the repeat var a = ContactType[i].value; var b = Phone[i].value; var c = Email[i].value; //identify which controls to send to the Google Sheet. Syntax is "GS column header":value. In this case our values are held in local variables. var params ={"firstname":fname,"lastname":lname,"ContactType":a,"Phone":b,"Email":c}; var updateparams = '&updatesjson='+encodeURIComponent(JSON.stringify(params)); //http.put updates the Google sheet eval('x=' + http.put(updatequery + updateparams, null, headers, false, false)); } }
- Replace the info shown in <> in these examples with your Google Sheet details (e.g. replace <spreadsheet key> with your spreadsheet's key, found in the spreadsheet URL.)
- In use, this form will
- Populate hidden lineNum table column with the last available line number from the Google Sheet, and increment that number for each row. It will then write back to the Google Sheet to update the last available line number (on the "getline" tab) so that if another form is opened before this one is submitted, they won't overwrite one another's submission. The image below shows the lineNum column for demo.
- When the trigger is clicked, update the Google Sheet rows that match the table's lineNum rows with the parameters you have set, which can include both form data outside the table and within the table for that specific row.
- Populate hidden lineNum table column with the last available line number from the Google Sheet, and increment that number for each row. It will then write back to the Google Sheet to update the last available line number (on the "getline" tab) so that if another form is opened before this one is submitted, they won't overwrite one another's submission. The image below shows the lineNum column for demo.
...