How to fill answer options from a private Google Sheet, and look up the chosen row

You can fill a step’s answer options from a Google Sheet, and look up other columns of the row your visitor chose, while the sheet stays private: it is read through your Google Drive integration on our server, and shared with nobody.

In this example the sheet is a property directory: a key, the property, its contact person and the contact’s e-mail address. The visitor picks a property; the report goes to that property’s contact, whose address the visitor never sees.

IdPropertyContactEmail
1Main Street 1Annaanna@example.com
2Harbour 5Bobbob@example.com

The first row is the header row. Give the sheet a key column of numbers (an id, or just a running number): the step’s answer keeps it, and the lookup reads it.

Step 1. Connect Google Drive and choose the sheet

In Organisation → Integrations, add the Google Drive integration, authorize it, and choose the spreadsheet in Google’s file picker. Quriobot can read only the files you choose there. The integration then shows the Google account it is connected to, on its page and after its name wherever you pick it, so you can tell which account a sheet is read from.

Step 2. A variable that lists the options

In your bot, under Settings → Variables, add a variable of type Spreadsheet query:

  • Name: properties
  • Google Drive integration, Spreadsheet and Sheet: the ones from step 1
  • Query: select A, B
  • Private: off — the visitor sees these names as the options anyway
The properties variable: a spreadsheet query that lists the options

The query is in the Google Sheets query language, on the sheet’s column letters. The variable’s value is the result, JSON-encoded: one column gives a list of values, several columns give one object per row, keyed by the header row ([{"Id":"1","Property":"Main Street 1"}, …]).

Step 3. The step’s options

On the Autocomplete step (or Image autocomplete, or Multiple choice), enable Populate options dynamically (developer mode) and use:

function (input, getVariableValue, callback) {
  var paged = input !== null && typeof input === 'object', q = ((paged ? input.input : input) || '').toLowerCase()
  var key = 'qb.properties.my-bot', show = function (rows) {
    var items = rows
      .filter(function (row) { return row.Property.toLowerCase().indexOf(q) !== -1 })
      .map(function (row) { return { value: row.Id, label: row.Property } })
    callback(paged ? { items: items.slice(input.offset, input.offset + input.limit), count: items.length } : items)
  }
  var rows = quriobot.store.get(key)
  if (rows) return show(rows)
  getVariableValue('properties', function (value) {
    rows = JSON.parse(value || '[]')
    quriobot.store.set(key, rows, new Date().getTime() + 60000)
    show(rows)
  })
}
The step's options script, under Populate options dynamically

quriobot.store keeps the list for a minute, so the bot asks for it once, not on every key the visitor types; replace my-bot with something of your own, so that two bots on one website keep their own lists. The same script works on all three steps: Autocomplete filters on what the visitor types, Image autocomplete also pages the list (add image: row.Image to show a picture column), and Multiple choice shows every row; set its Number of choices to let the visitor pick several.

The widget offers the rows that match what the visitor types

Step 4. A private lookup of the chosen row

Add a second Spreadsheet query variable:

  • Name: contact_email
  • Query: select D where A matches '{{property.value}}' (here property is the step’s code name)
  • Fallback: a general address, used when no row matches or the sheet cannot be read
  • Private: on
The contact_email variable: a private lookup of the chosen row

{{property.value}} is the key of the chosen option. Inside a matches comparison each chosen option is matched exactly, so the same query works for a Multiple choice step with several properties chosen. To look up by the shown text instead, use select D where B = '{{property}}'.

A private variable never reaches the visitor directly: chat texts show it empty, and the widget and scripts run in the visitor’s browser, such as the options script above, get nothing of it. Variables the server computes (a script resolved server-side, an AI prompt, another spreadsheet query) can use it; what they give is shown or kept by their own Private switch, so make them private too if their result must not reach the visitor. It is filled in in the bot’s response actions, such as an e-mail to you or a webhook, but not in an e-mail to the visitor (the Respondent template). Any custom variable can be private, not only a spreadsheet query: a Text variable holding an internal address, or a key a server-side script uses, for example. Only a step’s answer and a script run in the browser cannot be, since their value is already on the visitor’s side.

Step 5. Send the report to that address

The address fields take a comma-separated list, so add a Script variable contact_emails that turns the lookup’s result into one:

function (callback, variables) {
  var lookup = variables.contact_email && variables.contact_email.value
  var done = function (value) {
    var items
    try { items = JSON.parse(value || '[]') } catch (e) { return callback(value) }
    callback(Array.isArray(items) ? items.join(', ') : String(items))
  }
  typeof lookup === 'function' ? lookup(done) : done(lookup)
}
The contact_emails Script variable: the lookup as a comma-separated list

Then, in your e-mail response action, set To to {{contact_emails}}.

The e-mail response action sends the report to the contact_emails list

Good to know

  • A query’s result is kept for a minute and shared by all conversations: an edit of the sheet shows within a minute. Disable caching reads the sheet each time (Google limits how often a sheet can be read).
  • What a visitor types or answers is only ever compared as a value: it cannot change your query.
  • If the sheet cannot be read (access removed, file deleted, a query Google refuses), the variable gives its fallback, the error is in the variable’s log, and your organisation’s technical contacts get an e-mail.
  • The older recipe, How to populate answer options dynamically from the Google Sheets (direct integration), still works, but it needs the sheet shared as “Anyone with the link”: everything in such a sheet is public.

Edit this page