# GoogleTableSync

You can post, update and read data from GoogleSpreadSheet with this lib.

![](https://3310729168-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LVh0yg9olT-QWqi3AYt%2Fuploads%2FlxYW8ASrK1ow9QlEpwcd%2Fimage.png?alt=media\&token=1f4fd568-abc0-441d-b8d2-cf1c89fdce10)

### Setup

1\. Please see setup for [GoogleApp lib](https://help.bots.business/libs/googleapp). This lib requires GoogleApp lib.

2\. Create blank Google table. You don't need any headers. Headers will be added automatically.

3\. Also you need to run code too:

`Libs.GoogleApp.setUrl("https://script.google.com/*************/exec");`

{% hint style="info" %}
If you have any problems with this lib - use help from [GoogleApp lib](https://help.bots.business/libs/googleapp).
{% endhint %}

### Demo bot

{% embed url="<https://telegram.me/BBGoogleSpreadsheetBot>" %}

### Write or update data

{% hint style="success" %}
Use `index` for magic!

With index key exist row will be updated!
{% endhint %}

You need tableID. You can get it from table url. It is selected here:

![](https://3310729168-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LVh0yg9olT-QWqi3AYt%2Fuploads%2FrGBhLX1GfBJZzGWVGTQo%2Fimage.png?alt=media\&token=07c89dce-7df7-4632-9abb-ceeaa9b6c581)

```javascript
Bot.sendMessage("Saving...");

var syncOptions = {
  tableID: "1_NldI2**********ank1B9c",
  sheetName: "Users",
  // this column will be used as index for updates or reading
  index: "id",
  // store data
  datas: [],
  // this command will be runned after sync
  onRun: "/onSync",

  // for debug. Comment this lines
  // email: "hello@bots.business",
  // debug: true
}

user.balance = User.getProperty("balance");

syncOptions.datas[0] = user;

// you can add more records with index 
// syncOptions.datas[1] and etc 

Libs.GoogleTableSync.sync(syncOptions);
```

{% hint style="success" %}
You can store any data like users, chats, products, resources and etc
{% endhint %}

```javascript
Bot.sendMessage("Saving...");

var syncOptions = {
  tableID: "1_NldI2**********ank1B9c",
  sheetName: "Orders",
  // this column will be used as index for updates or reading
  index: "orderId",
  datas: [],
  // this command will be runned after sync
  onRun: "/onSync"
}

var order = {
   orderId: 10,
   title: "Order - " + String(user.id),
   amount: 15
}

syncOptions.datas.push(order);
Libs.GoogleTableSync.sync(syncOptions);
```

### Read data

```javascript
Bot.sendMessage("Reading...");

var syncOptions = {
  tableID: "1_NldI2**********ank1B9c",
  sheetName: "Users",
  // this column will be used as index for updates or reading
  index: "id",
  // reading data
  datas: [],
  // this command will be runned after sync
  onRun: "/onSync",

  // for debug. Comment this lines
  // email: "hello@bots.business",
  // debug: true
}

syncOptions.datas[0] = user;
// also you can use something like:
// syncOptions.datas[0] = { id: any_user_id }

// you can add more records with index 
// syncOptions.datas[1] and etc 


Libs.GoogleTableSync.read(syncOptions)
```

### Command /onSync

`Bot.inspect(options)`
