I am trying to write data to a Google Sheet using the Nodejs API v4. I can sucessfully read and clear data, so I have set everything up correctly. However, using the example in the docs here for the update method, I cannot work out how to specify the data I want to put in the Google sheet. I would like an example showing how to paste the data held by const data
into the sheet specified. Here is what I have so far:
JavaScript
x
18
18
1
const data = [
2
[1, 2],
3
[3, 4],
4
];
5
const auth = new google.auth.GoogleAuth({
6
scopes: ["https://www.googleapis.com/auth/spreadsheets"],
7
});
8
const authClient = await auth.getClient();
9
const sheets = google.sheets({ version: "v4", auth: authClient });
10
const request = {
11
spreadsheetId: "my-spreadsheet-id",
12
range: "Sheet1!A:E",
13
valueInputOption: "",
14
resource: {},
15
auth: authClient,
16
};
17
const response = await sheets.spreadsheets.values.update(request);
18
Advertisement
Answer
You must specify a value input option
Possible values are:
USER_ENTERED
RAW
INPUT_VALUE_OPTION_UNSPECIFIED
The resource is your data array.
Sample:
JavaScript
1
11
11
1
const request = {
2
spreadsheetId: "my-spreadsheet-id",
3
range: "Sheet1!A:E",
4
valueInputOption: "USER_ENTERED",
5
resource: {values: [
6
[1, 2],
7
[3, 4],
8
]},
9
auth: authClient,
10
};
11