# Using Google Sheets

## How to Install the Plugin

Before installing the plugin, create a service account in your Google account.  To do this:

* Visit [https://console.cloud.google.com/apis/dashboard](https://console.cloud.google.com/apis/dashboard?pli=1)
* Click to Credentials -> Create Credentials -> Service Account

<figure><img src="/files/WWXTuYPlreMBvBXK7MVt" alt=""><figcaption></figcaption></figure>

* Add the 'Editor' role for the new account

![](/files/89IQ3KQrGYkdVRKXizjx)

* After creating the service account, go to the Service Account section, find your account, and click Manage Key

![](/files/YbE9kVitohv48lLVpDib)

* Add new key pairs

![](/files/DxE5UXNw4a7g7bTePeq9)

* Choose the **JSON** format

![](/files/oOHcPR6orUJOjIrMds8l)

After creating the key, Google will send a JSON file to your computer. Please open it and copy the JSON string from this file.

* Enable Google API: <https://console.cloud.google.com/apis/api/sheets.googleapis.com>

### Insert the JSON service account key into the plugin

1. Go to the plugin section
2. In the Scenario step section, find Google Sheets
3. Insert your JSON key from your Google account into the 'fieldWithJSON' field.

![](/files/QtwfJuk97d1GzjklIQMu)

## How to Use the Plugin in the SDK?

### Basic Methods

```
//we could creating document
var xls = $D.sheet.create("test") 
//or you could open existing document
//var xls = "https://docs.google.com/spreadsheets/d/XXX-XXX-SSS/edit"

//now, we could read this xls document first sheet (first tab in XLS)
var data  = $D.sheet.read(xls, "Sheet1")
//output: data = [ [1,2,3] ]


//also, we could write document as
$D.sheet.write(xls, "Sheet1", [ [1,2,3], [4,5,6] ])

```

### Additional Methods Similar to Google Scripts

```
var ss = $D.sheet.open(xls) //xls link like this: "https://docs.google.com/spreadsheets/d/XXX-XXX-SSS/edit"

//get first page in the sheets
var sheet = ss.getSheets()[0]; 
//show getLastRow and getLastColumn in sheet
$D.console.log("sheet.getLastRow:" + sheet.getLastRow()) 
$D.console.log("sheet.getLastColumn:" + sheet.getLastColumn())

//read values
var values = sheet.getRange('B2:D4').getValues()
for(var r = 0; r<values.length; r++) {
    var row = values[r]
    for(var c = 0; c<row.length; c++) {
      var value = row[c]
      $D.console.log("row:" + r + " c:" + c + " value:" + value)
    }
 }

 //write as google scripts  
  var values = [
    [ "2.000", "1,000,000", "$2.99" ]
  ];
  var range = sheet.getRange("B2:D2");
  range.setValues(values);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://readme.directual.com/javascript-sdk/using-google-sheets.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
