# Google Sheets

The search in a spreadsheet: a formula that lists the websites whose page source contains a fragment of code, and a menu that exports a whole query to a new sheet. Free to copy, runs with your API token.

```
=PUBLICWWW(A1)                    websites whose page source contains the query in A1
=PUBLICWWW(A1, 50, "url,rank")    50 rows, only the url and rank columns
=PUBLICWWW_COUNT(A1)              how many websites match
```

## Setting it up

1. Open a spreadsheet, then **Extensions > Apps Script**.
2. Replace the contents of `Code.gs` with [Code.gs](https://github.com/publicwww-com/publicwww-google-sheets/blob/main/Code.gs) from [the repository](https://github.com/publicwww-com/publicwww-google-sheets) and save.
3. Reload the spreadsheet. A **PublicWWW** menu appears; choose **Set API token** and paste a token from your [profile page](https://publicwww.com/profile/api). The API needs a paid plan.
4. Google asks once to authorize the script: it reads and writes this spreadsheet only, and connects to `api.publicwww.com`.

The token is kept in the script's properties, so anyone who can edit this spreadsheet's script can read it. Share edit access accordingly, or give the spreadsheet a token of its own that you can delete on the profile page.

## Writing the query

The query is the same as in the search box - see [query syntax](https://publicwww.com/docs/query-syntax/). Quotes matter: `"angular.min.js"` with the quotes is that exact string, without them it is several words. Inside a formula a quote is written twice, `=PUBLICWWW("""angular.min.js""")`, so it is easier to type the query, quotes included, into a cell and refer to it: `=PUBLICWWW(A1)`.

## The functions

- `PUBLICWWW(query, [rows], [columns], [header])` - one website per row, the most popular first. `rows` - 10 by default, up to 1000 in a formula. `columns` - any of `domain`, `url`, `rank`, comma separated; all three by default. `header` - `TRUE` puts the column names on the first row. `rank` is empty for a website without a popularity rank; nothing found gives an empty cell.
- `PUBLICWWW_COUNT(query)` - the number of matching websites.

A formula runs a search when it is entered or its arguments change, and an answer is kept for six hours, so reopening the sheet does not spend searches again. Each new formula is one search from the plan's daily quota.

## Exporting a whole query

**PublicWWW > Export a query to a new sheet** writes every row your plan covers for the query into a new sheet, with the totals in a note on its first cell. A very large export can run into Google's limit of six minutes per script run; for lists of hundreds of thousands of rows, ask the [API](https://publicwww.com/docs/api/requests/) directly - `format=csv` opens in any spreadsheet.

## When a cell says #ERROR!

Hover over the cell for the reason.

| Reason | What to do |
| --- | --- |
| `No API token yet` | PublicWWW > Set API token. |
| `invalid_key` | The token was deleted; set a new one. |
| `plan_required` | The API needs a [paid plan](https://publicwww.com/prices.html). |
| `too_many_requests` | More than 10 requests a minute; wait the seconds shown. |
| `quota_exceeded` | The day's searches are used up; they reset at midnight UTC. |

All codes: [Errors](https://publicwww.com/docs/api/errors/).

Source: https://publicwww.com/docs/api/sheets/
