Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .changeset/new-things-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
'@openfn/language-googlesheets': major
---

Updated `appendValues()`, `batchUpdateValues()`, and `getValues()` to use positional arguments instead of a single params object.
Comment thread
hunterachieng marked this conversation as resolved.

### Migration Guide

**`appendValues`**

```js
// Before
appendValues({
spreadsheetId: '1abc...',
range: 'Sheet1!A1:E1',
values: [['a', 'b']],
});

// Now
appendValues('1abc...', { range: 'Sheet1!A1:E1', values: [['a', 'b']] });
```

**`batchUpdateValues`**

```js
// Before
batchUpdateValues({
spreadsheetId: '1abc...',
range: 'Sheet1!A1',
values: [['a']],
valueInputOption: 'RAW',
});

// Now — accepts an array of range/values objects
batchUpdateValues(
'1abc...',
[{ range: 'Sheet1!A1', values: [['a']] }],
{ valueInputOption: 'RAW' }
);
```

**`getValues`**

Signature unchanged. Callback parameter has been removed; use `fn()` to transform the response instead.

24 changes: 21 additions & 3 deletions packages/googlesheets/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@
"tags": [
{
"title": "example",
"description": "batchUpdateValues({\n spreadsheetId: '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',\n range: 'Sheet1!A1:E1',\n values: [\n ['From expression', '$15', '2', '3/15/2016'],\n ['Really now!', '$100', '1', '3/20/2016'],\n ],\n})"
"description": "batchUpdateValues({\n spreadsheetId: '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',\n range: 'Sheet1!A1:E1',\n values: [\n ['From expression', '$15', '2', '3/15/2016'],\n ['Really now!', '$100', '1', '3/20/2016'],\n ],\n})",
"caption": "Update a single range"
},
{
"title": "example",
"description": "batchUpdateValues({\n spreadsheetId: '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',\n data: [\n { range: 'Sheet1!A1', values: [['value1']] },\n { range: 'Sheet1!B5', values: [['value2']] },\n { range: 'Sheet1!D10:E11', values: [['a', 'b'], ['c', 'd']] },\n ],\n valueInputOption: 'RAW',\n})",
"caption": "Update multiple ranges"
},
{
"title": "function",
Expand Down Expand Up @@ -135,7 +141,7 @@
},
{
"title": "param",
"description": "The range of values to update.",
"description": "The range of values to update (single-range form).",
"type": {
"type": "OptionalType",
"expression": {
Expand All @@ -159,7 +165,7 @@
},
{
"title": "param",
"description": "A 2d array of values to update.",
"description": "A 2d array of values to update (single-range form).",
"type": {
"type": "OptionalType",
"expression": {
Expand All @@ -169,6 +175,18 @@
},
"name": "params.values"
},
{
"title": "param",
"description": "An array of ValueRange objects `({ range, values })` for updating multiple ranges at once.",
"type": {
"type": "OptionalType",
"expression": {
"type": "NameExpression",
"name": "array"
}
},
"name": "params.data"
},
{
"title": "param",
"description": "(Optional) callback function",
Expand Down
3 changes: 2 additions & 1 deletion packages/googlesheets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"chai": "4.3.6",
"deep-eql": "4.1.1",
"nock": "13.2.9",
"rimraf": "3.0.2"
"rimraf": "3.0.2",
"sinon": "^21.1.2"
},
"type": "module",
"types": "types/index.d.ts",
Expand Down
115 changes: 63 additions & 52 deletions packages/googlesheets/src/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,36 @@ export function execute(...operations) {
* https://developers.google.com/sheets/api/samples/writing#append_values
* @public
* @example
* appendValues({
* spreadsheetId: '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',
* range: 'Sheet1!A1:E1',
* values: [
* ['From expression', '$15', '2', '3/15/2016'],
* ['Really now!', '$100', '1', '3/20/2016'],
* ],
* })
* appendValues(
Comment thread
mtuchi marked this conversation as resolved.
* '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',
* {
* range: 'Sheet1!A1:E1',
* values: [
* ['From expression', '$15', '2', '3/15/2016'],
* ['Really now!', '$100', '1', '3/20/2016'],
* ],
* }
* )
* @function
* @param {Object} params - Data object to add to the spreadsheet.
* @param {string} [params.spreadsheetId] The spreadsheet ID.
* @param {string} [params.range] The range of values to update.
* @param {array} [params.values] A 2d array of values to update.
* @param {function} callback - (Optional) Callback function
* @param {string} spreadsheetId - The spreadsheet ID.
* @param {Object} data - Data to append.
* @param {string} data.range - The range to append to.
* @param {array} data.values - A 2d array of values to append.
* @param {Object} [options] - Optional settings.
* @param {string} [options.valueInputOption] - Defaults to 'USER_ENTERED'.
* @param {function} [callback] - Optional callback function.
* @returns {Operation}
*/
export function appendValues(params, callback = s => s) {
export function appendValues(spreadsheetId, data, options = {}, callback = s => s) {
Comment thread
hunterachieng marked this conversation as resolved.
Outdated
return state => {
const [resolvedParams] = expandReferences(state, params);
const { spreadsheetId, range, values } = resolvedParams;
const [resolvedSpreadsheetId, resolvedData, resolvedOptions] = expandReferences(
state,
spreadsheetId,
data,
options
);
const { range, values } = resolvedData;
Comment thread
hunterachieng marked this conversation as resolved.
Outdated
const { valueInputOption = 'USER_ENTERED' } = resolvedOptions;

if (!values || values.length === 0) {
console.log('Warning: empty values array');
Expand All @@ -107,13 +117,13 @@ export function appendValues(params, callback = s => s) {
return new Promise((resolve, reject) => {
client.spreadsheets.values.append(
{
spreadsheetId,
spreadsheetId: resolvedSpreadsheetId,
range,
valueInputOption: 'USER_ENTERED',
valueInputOption,
resource: {
range,
majorDimension: 'ROWS',
values: values,
values,
},
},
function (err, response) {
Expand All @@ -139,52 +149,54 @@ export function appendValues(params, callback = s => s) {
/**
* Batch update values in a Spreadsheet.
* @example
* batchUpdateValues({
* spreadsheetId: '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',
* range: 'Sheet1!A1:E1',
* values: [
* ['From expression', '$15', '2', '3/15/2016'],
* ['Really now!', '$100', '1', '3/20/2016'],
* <caption>Update a single range</caption>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice for the single range API to take a single object, rather than an array of one. Just a little API convenience

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josephjclark can you say more 🤔 , values are rows data do you mean we should pass values: [{columnHeader: value, colHeader: value}, {...}]?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So multiple ranges is:

batchUpdateValues(id, [set1, set2])

Where a set is { range, values }
And a single range so far is:

batchUpdateValues(id, [set1])

I'm saying allow a single range to be passed like this:

batchUpdateValues(id, set1)

BUT THEN AGAIN

That makes the API the same as the appendValues API, which only confuses the two.

Ok, so we should force data to be an array of ranges. But do not show an example with an array of just 1 range because it's wierd

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think function naming is what causing the confusion here. There are singular actions and plural actions
For example:

  • If we had appendValue(id, range, data, opt) this is clear, add new row to gsheet
  • For appendValues(id, [{set0, set1}], opt), this is clear adding multiple rows

The same could apply for batchUpdateValues()

  • batchUpdate(id, range, value,opt)
  • batchUpdate(id, [{set0}, {set1}])

Of course adding good docs that explain what append does, and what does batchUpdate do? because their different.

I will remove the single batch update example

* batchUpdateValues(
* '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',
* [{ range: 'Sheet1!A1:E1', values: [['From expression', '$15'], ['Really now!', '$100']] }],
* { valueInputOption: 'RAW' }
* )
* @example
* <caption>Update multiple non-contiguous ranges</caption>
Comment thread
hunterachieng marked this conversation as resolved.
Outdated
* batchUpdateValues(
* '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',
* [
* { range: 'Sheet1!A1', values: [['value1']] },
* { range: 'Sheet1!B5', values: [['value2']] },
* { range: 'Sheet1!D10:E11', values: [['a', 'b'], ['c', 'd']] },
* ],
* })
* { valueInputOption: 'RAW' }
* )
* @function
* @public
* @param {Object} params - Data object to add to the spreadsheet.
* @param {string} [params.spreadsheetId] The spreadsheet ID.
* @param {string} [params.range] The range of values to update.
* @param {string} [params.valueInputOption] (Optional) Value update options. Defaults to 'USER_ENTERED'
* @param {array} [params.values] A 2d array of values to update.
* @param {function} callback - (Optional) callback function
* @param {string} spreadsheetId - The spreadsheet ID.
* @param {Array<{range: string, values: array}>} data - Array of range/values objects to update.
* @param {Object} [options] - Optional settings.
* @param {string} [options.valueInputOption] - Defaults to 'USER_ENTERED'.
* @param {function} [callback] - Optional callback function.
* @returns {Operation} spreadsheet information
*/
export function batchUpdateValues(params, callback = s => s) {
export function batchUpdateValues(spreadsheetId, data, options = {}, callback = s => s) {
return async state => {
const [resolvedParams] = expandReferences(state, params);

const {
const [resolvedSpreadsheetId, resolvedData, resolvedOptions] = expandReferences(
state,
spreadsheetId,
range,
valueInputOption = 'USER_ENTERED',
values,
} = resolvedParams;
data,
options
);
const { valueInputOption = 'USER_ENTERED' } = resolvedOptions;

if (!values || values.length === 0) {
console.log('Warning: empty values array');
if (!resolvedData || resolvedData.length === 0) {
console.log('Warning: empty data array');
return state;
}

const resource = {
data: [
{
range,
values,
},
],
data: resolvedData,
valueInputOption,
};
try {
const response = await client.spreadsheets.values.batchUpdate({
spreadsheetId,
spreadsheetId: resolvedSpreadsheetId,
resource,
});
console.log('%d cells updated.', response.data.totalUpdatedCells);
Expand All @@ -204,10 +216,9 @@ export function batchUpdateValues(params, callback = s => s) {
* @function
* @param {string} spreadsheetId The spreadsheet ID.
* @param {string} range The sheet range.
* @param {function} callback - (Optional) callback function
* @returns {Operation} spreadsheet information
*/
export function getValues(spreadsheetId, range, callback = s => s) {
export function getValues(spreadsheetId, range ) {
return async state => {
const [resolvedSheetId, resolvedRange] = expandReferences(
state,
Expand All @@ -225,7 +236,7 @@ export function getValues(spreadsheetId, range, callback = s => s) {

const nextState = { ...composeNextState(state, response.data), response };

return callback(nextState);
return nextState;
} catch (err) {
logError(err);
throw err;
Expand Down
Loading
Loading