-
Notifications
You must be signed in to change notification settings - Fork 33
gsheets: Update input data format and allow for multiple range updates #1655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
c64ad93
2233968
5e89d3c
1066dd3
49de380
fd518f7
8248c1d
af3b8cf
ace54dd
ae5d435
ac836aa
84a8311
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
|
|
||
| ### 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. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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( | ||
|
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) { | ||
|
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; | ||
|
hunterachieng marked this conversation as resolved.
Outdated
|
||
| const { valueInputOption = 'USER_ENTERED' } = resolvedOptions; | ||
|
|
||
| if (!values || values.length === 0) { | ||
| console.log('Warning: empty values array'); | ||
|
|
@@ -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) { | ||
|
|
@@ -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> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So multiple ranges is: Where a set is I'm saying allow a single range to be passed like this: BUT THEN AGAIN That makes the API the same as the 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
The same could apply for
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> | ||
|
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); | ||
|
|
@@ -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, | ||
|
|
@@ -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; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.