@openfn/language-googlesheets 2.2.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ast.json CHANGED
@@ -3,7 +3,8 @@
3
3
  {
4
4
  "name": "appendValues",
5
5
  "params": [
6
- "params"
6
+ "params",
7
+ "callback"
7
8
  ],
8
9
  "docs": {
9
10
  "description": "Add an array of rows to the spreadsheet.\nhttps://developers.google.com/sheets/api/samples/writing#append_values",
@@ -31,6 +32,42 @@
31
32
  },
32
33
  "name": "params"
33
34
  },
35
+ {
36
+ "title": "param",
37
+ "description": "The spreadsheet ID.",
38
+ "type": {
39
+ "type": "OptionalType",
40
+ "expression": {
41
+ "type": "NameExpression",
42
+ "name": "string"
43
+ }
44
+ },
45
+ "name": "params.spreadsheetId"
46
+ },
47
+ {
48
+ "title": "param",
49
+ "description": "The range of values to update.",
50
+ "type": {
51
+ "type": "OptionalType",
52
+ "expression": {
53
+ "type": "NameExpression",
54
+ "name": "string"
55
+ }
56
+ },
57
+ "name": "params.range"
58
+ },
59
+ {
60
+ "title": "param",
61
+ "description": "A 2d array of values to update.",
62
+ "type": {
63
+ "type": "OptionalType",
64
+ "expression": {
65
+ "type": "NameExpression",
66
+ "name": "array"
67
+ }
68
+ },
69
+ "name": "params.values"
70
+ },
34
71
  {
35
72
  "title": "returns",
36
73
  "description": null,
@@ -41,7 +78,61 @@
41
78
  }
42
79
  ]
43
80
  },
44
- "valid": true
81
+ "valid": false
82
+ },
83
+ {
84
+ "name": "getValues",
85
+ "params": [
86
+ "spreadsheetId",
87
+ "range",
88
+ "callback"
89
+ ],
90
+ "docs": {
91
+ "description": "Gets cell values from a Spreadsheet.",
92
+ "tags": [
93
+ {
94
+ "title": "public",
95
+ "description": null,
96
+ "type": null
97
+ },
98
+ {
99
+ "title": "example",
100
+ "description": "getValues('1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos','Sheet1!A1:E1')"
101
+ },
102
+ {
103
+ "title": "function",
104
+ "description": null,
105
+ "name": null
106
+ },
107
+ {
108
+ "title": "param",
109
+ "description": "The spreadsheet ID.",
110
+ "type": {
111
+ "type": "NameExpression",
112
+ "name": "string"
113
+ },
114
+ "name": "spreadsheetId"
115
+ },
116
+ {
117
+ "title": "param",
118
+ "description": "The sheet range.",
119
+ "type": {
120
+ "type": "NameExpression",
121
+ "name": "string"
122
+ },
123
+ "name": "range"
124
+ },
125
+ {
126
+ "title": "returns",
127
+ "description": "spreadsheet information",
128
+ "type": {
129
+ "type": "NameExpression",
130
+ "name": "Operation"
131
+ }
132
+ }
133
+ ]
134
+ },
135
+ "valid": false
45
136
  }
46
137
  ],
47
138
  "exports": [],
@@ -1,20 +1,19 @@
1
1
  {
2
- "$schema": "http://json-schema.org/draft-07/schema#",
3
- "properties": {
4
- "accessToken": {
5
- "title": "Access Token",
6
- "type": "string",
7
- "description": "Your Google Sheets access token",
8
- "writeOnly": true,
9
- "minLength": 1,
10
- "examples": [
11
- "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjlGWERwYmZNRlQyU3ZRdVhoODQ2WVR3RUlCdyIsI"
12
- ]
13
- }
14
- },
15
- "type": "object",
16
- "additionalProperties": true,
17
- "required": [
18
- "accessToken"
19
- ]
20
- }
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$comment": "OAuth2",
4
+ "properties": {
5
+ "access_token": {
6
+ "title": "Access Token",
7
+ "type": "string",
8
+ "description": "Your Google Sheets access token",
9
+ "writeOnly": true,
10
+ "minLength": 1,
11
+ "examples": [
12
+ "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjlGWERwYmZNRlQyU3ZRdVhoODQ2WVR3RUlCdyIsI"
13
+ ]
14
+ }
15
+ },
16
+ "type": "object",
17
+ "additionalProperties": true,
18
+ "required": ["access_token"]
19
+ }
package/dist/index.cjs CHANGED
@@ -21,6 +21,7 @@ var src_exports = {};
21
21
  __export(src_exports, {
22
22
  alterState: () => import_language_common2.alterState,
23
23
  appendValues: () => appendValues,
24
+ batchUpdateValues: () => batchUpdateValues,
24
25
  combine: () => import_language_common2.combine,
25
26
  dataPath: () => import_language_common2.dataPath,
26
27
  dataValue: () => import_language_common2.dataValue,
@@ -30,6 +31,7 @@ __export(src_exports, {
30
31
  field: () => import_language_common2.field,
31
32
  fields: () => import_language_common2.fields,
32
33
  fn: () => import_language_common2.fn,
34
+ getValues: () => getValues,
33
35
  http: () => import_language_common2.http,
34
36
  lastReferenceValue: () => import_language_common2.lastReferenceValue,
35
37
  merge: () => import_language_common2.merge,
@@ -42,6 +44,7 @@ var Adaptor_exports = {};
42
44
  __export(Adaptor_exports, {
43
45
  alterState: () => import_language_common2.alterState,
44
46
  appendValues: () => appendValues,
47
+ batchUpdateValues: () => batchUpdateValues,
45
48
  combine: () => import_language_common2.combine,
46
49
  dataPath: () => import_language_common2.dataPath,
47
50
  dataValue: () => import_language_common2.dataValue,
@@ -50,34 +53,63 @@ __export(Adaptor_exports, {
50
53
  field: () => import_language_common2.field,
51
54
  fields: () => import_language_common2.fields,
52
55
  fn: () => import_language_common2.fn,
56
+ getValues: () => getValues,
53
57
  http: () => import_language_common2.http,
54
58
  lastReferenceValue: () => import_language_common2.lastReferenceValue,
55
59
  merge: () => import_language_common2.merge,
56
60
  sourceValue: () => import_language_common2.sourceValue
57
61
  });
58
62
  var import_language_common = require("@openfn/language-common");
63
+ var import_util = require("@openfn/language-common/util");
59
64
  var import_googleapis = require("googleapis");
60
65
  var import_language_common2 = require("@openfn/language-common");
66
+ var client = void 0;
67
+ function createConnection(state) {
68
+ const { accessToken } = state.configuration;
69
+ const auth = new import_googleapis.google.auth.OAuth2();
70
+ auth.credentials = { access_token: accessToken };
71
+ client = import_googleapis.google.sheets({ version: "v4", auth });
72
+ return state;
73
+ }
74
+ function removeConnection(state) {
75
+ client = void 0;
76
+ return state;
77
+ }
78
+ function logError(err) {
79
+ const { code, errors, response } = err;
80
+ if (code && errors && response) {
81
+ console.error("The API returned an error:", errors);
82
+ const { statusText, config } = response;
83
+ const { url, method, body } = config;
84
+ const message = `${method} ${url} - ${code}:${statusText}
85
+ body: ${body}`;
86
+ console.log(message);
87
+ }
88
+ }
61
89
  function execute(...operations) {
62
90
  const initialState = {
63
91
  references: [],
64
92
  data: null
65
93
  };
66
94
  return (state) => {
67
- return (0, import_language_common.execute)(...operations)({ ...initialState, ...state });
95
+ return (0, import_language_common.execute)(
96
+ createConnection,
97
+ ...operations,
98
+ removeConnection
99
+ )({
100
+ ...initialState,
101
+ ...state,
102
+ configuration: (0, import_util.normalizeOauthConfig)(state.configuration)
103
+ });
68
104
  };
69
105
  }
70
- function appendValues(params) {
106
+ function appendValues(params, callback = (s) => s) {
71
107
  return (state) => {
72
- const { accessToken } = state.configuration;
73
- const oauth2Client = new import_googleapis.google.auth.OAuth2();
74
- oauth2Client.credentials = { access_token: accessToken };
75
- const { spreadsheetId, range, values } = (0, import_language_common.expandReferences)(params)(state);
76
- var sheets = import_googleapis.google.sheets("v4");
108
+ const [resolvedParams] = (0, import_util.expandReferences)(state, params);
109
+ const { spreadsheetId, range, values } = resolvedParams;
77
110
  return new Promise((resolve, reject) => {
78
- sheets.spreadsheets.values.append(
111
+ client.spreadsheets.values.append(
79
112
  {
80
- auth: oauth2Client,
81
113
  spreadsheetId,
82
114
  range,
83
115
  valueInputOption: "USER_ENTERED",
@@ -89,19 +121,77 @@ function appendValues(params) {
89
121
  },
90
122
  function(err, response) {
91
123
  if (err) {
92
- console.log("The API returned an error:");
93
- console.log(err);
124
+ logError(err);
94
125
  reject(err);
95
126
  } else {
96
127
  console.log("Success! Here is the response from Google:");
97
- console.log(response);
98
- resolve(state);
128
+ console.log(response.data);
129
+ resolve(
130
+ callback({
131
+ ...(0, import_language_common.composeNextState)(state, response.data),
132
+ response
133
+ })
134
+ );
99
135
  }
100
136
  }
101
137
  );
102
138
  });
103
139
  };
104
140
  }
141
+ function batchUpdateValues(params, callback = (s) => s) {
142
+ return async (state) => {
143
+ const [resolvedParams] = (0, import_util.expandReferences)(state, params);
144
+ const {
145
+ spreadsheetId,
146
+ range,
147
+ valueInputOption = "USER_ENTERED",
148
+ values
149
+ } = resolvedParams;
150
+ const resource = {
151
+ data: [
152
+ {
153
+ range,
154
+ values
155
+ }
156
+ ],
157
+ valueInputOption
158
+ };
159
+ try {
160
+ const response = await client.spreadsheets.values.batchUpdate({
161
+ spreadsheetId,
162
+ resource
163
+ });
164
+ console.log("%d cells updated.", response.data.totalUpdatedCells);
165
+ return callback({ ...(0, import_language_common.composeNextState)(state, response.data), response });
166
+ } catch (err) {
167
+ logError(err);
168
+ throw err;
169
+ }
170
+ };
171
+ }
172
+ function getValues(spreadsheetId, range, callback = (s) => s) {
173
+ return async (state) => {
174
+ var _a;
175
+ const [resolvedSheetId, resolvedRange] = (0, import_util.expandReferences)(
176
+ state,
177
+ spreadsheetId,
178
+ range
179
+ );
180
+ try {
181
+ const response = await client.spreadsheets.values.get({
182
+ spreadsheetId: resolvedSheetId,
183
+ range: resolvedRange
184
+ });
185
+ const numRows = ((_a = response == null ? void 0 : response.data) == null ? void 0 : _a.values.length) ?? 0;
186
+ console.log(`${numRows} rows retrieved.`);
187
+ const nextState = { ...(0, import_language_common.composeNextState)(state, response.data), response };
188
+ return callback(nextState);
189
+ } catch (err) {
190
+ logError(err);
191
+ throw err;
192
+ }
193
+ };
194
+ }
105
195
 
106
196
  // src/index.js
107
197
  var src_default = Adaptor_exports;
@@ -109,6 +199,7 @@ var src_default = Adaptor_exports;
109
199
  0 && (module.exports = {
110
200
  alterState,
111
201
  appendValues,
202
+ batchUpdateValues,
112
203
  combine,
113
204
  dataPath,
114
205
  dataValue,
@@ -117,6 +208,7 @@ var src_default = Adaptor_exports;
117
208
  field,
118
209
  fields,
119
210
  fn,
211
+ getValues,
120
212
  http,
121
213
  lastReferenceValue,
122
214
  merge,
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ var Adaptor_exports = {};
9
9
  __export(Adaptor_exports, {
10
10
  alterState: () => alterState,
11
11
  appendValues: () => appendValues,
12
+ batchUpdateValues: () => batchUpdateValues,
12
13
  combine: () => combine,
13
14
  dataPath: () => dataPath,
14
15
  dataValue: () => dataValue,
@@ -17,6 +18,7 @@ __export(Adaptor_exports, {
17
18
  field: () => field,
18
19
  fields: () => fields,
19
20
  fn: () => fn,
21
+ getValues: () => getValues,
20
22
  http: () => http,
21
23
  lastReferenceValue: () => lastReferenceValue,
22
24
  merge: () => merge,
@@ -24,8 +26,12 @@ __export(Adaptor_exports, {
24
26
  });
25
27
  import {
26
28
  execute as commonExecute,
27
- expandReferences
29
+ composeNextState
28
30
  } from "@openfn/language-common";
31
+ import {
32
+ normalizeOauthConfig,
33
+ expandReferences
34
+ } from "@openfn/language-common/util";
29
35
  import { google } from "googleapis";
30
36
  import {
31
37
  alterState,
@@ -41,26 +47,53 @@ import {
41
47
  merge,
42
48
  sourceValue
43
49
  } from "@openfn/language-common";
50
+ var client = void 0;
51
+ function createConnection(state) {
52
+ const { accessToken } = state.configuration;
53
+ const auth = new google.auth.OAuth2();
54
+ auth.credentials = { access_token: accessToken };
55
+ client = google.sheets({ version: "v4", auth });
56
+ return state;
57
+ }
58
+ function removeConnection(state) {
59
+ client = void 0;
60
+ return state;
61
+ }
62
+ function logError(err) {
63
+ const { code, errors, response } = err;
64
+ if (code && errors && response) {
65
+ console.error("The API returned an error:", errors);
66
+ const { statusText, config } = response;
67
+ const { url, method, body } = config;
68
+ const message = `${method} ${url} - ${code}:${statusText}
69
+ body: ${body}`;
70
+ console.log(message);
71
+ }
72
+ }
44
73
  function execute(...operations) {
45
74
  const initialState = {
46
75
  references: [],
47
76
  data: null
48
77
  };
49
78
  return (state) => {
50
- return commonExecute(...operations)({ ...initialState, ...state });
79
+ return commonExecute(
80
+ createConnection,
81
+ ...operations,
82
+ removeConnection
83
+ )({
84
+ ...initialState,
85
+ ...state,
86
+ configuration: normalizeOauthConfig(state.configuration)
87
+ });
51
88
  };
52
89
  }
53
- function appendValues(params) {
90
+ function appendValues(params, callback = (s) => s) {
54
91
  return (state) => {
55
- const { accessToken } = state.configuration;
56
- const oauth2Client = new google.auth.OAuth2();
57
- oauth2Client.credentials = { access_token: accessToken };
58
- const { spreadsheetId, range, values } = expandReferences(params)(state);
59
- var sheets = google.sheets("v4");
92
+ const [resolvedParams] = expandReferences(state, params);
93
+ const { spreadsheetId, range, values } = resolvedParams;
60
94
  return new Promise((resolve, reject) => {
61
- sheets.spreadsheets.values.append(
95
+ client.spreadsheets.values.append(
62
96
  {
63
- auth: oauth2Client,
64
97
  spreadsheetId,
65
98
  range,
66
99
  valueInputOption: "USER_ENTERED",
@@ -72,25 +105,84 @@ function appendValues(params) {
72
105
  },
73
106
  function(err, response) {
74
107
  if (err) {
75
- console.log("The API returned an error:");
76
- console.log(err);
108
+ logError(err);
77
109
  reject(err);
78
110
  } else {
79
111
  console.log("Success! Here is the response from Google:");
80
- console.log(response);
81
- resolve(state);
112
+ console.log(response.data);
113
+ resolve(
114
+ callback({
115
+ ...composeNextState(state, response.data),
116
+ response
117
+ })
118
+ );
82
119
  }
83
120
  }
84
121
  );
85
122
  });
86
123
  };
87
124
  }
125
+ function batchUpdateValues(params, callback = (s) => s) {
126
+ return async (state) => {
127
+ const [resolvedParams] = expandReferences(state, params);
128
+ const {
129
+ spreadsheetId,
130
+ range,
131
+ valueInputOption = "USER_ENTERED",
132
+ values
133
+ } = resolvedParams;
134
+ const resource = {
135
+ data: [
136
+ {
137
+ range,
138
+ values
139
+ }
140
+ ],
141
+ valueInputOption
142
+ };
143
+ try {
144
+ const response = await client.spreadsheets.values.batchUpdate({
145
+ spreadsheetId,
146
+ resource
147
+ });
148
+ console.log("%d cells updated.", response.data.totalUpdatedCells);
149
+ return callback({ ...composeNextState(state, response.data), response });
150
+ } catch (err) {
151
+ logError(err);
152
+ throw err;
153
+ }
154
+ };
155
+ }
156
+ function getValues(spreadsheetId, range, callback = (s) => s) {
157
+ return async (state) => {
158
+ var _a;
159
+ const [resolvedSheetId, resolvedRange] = expandReferences(
160
+ state,
161
+ spreadsheetId,
162
+ range
163
+ );
164
+ try {
165
+ const response = await client.spreadsheets.values.get({
166
+ spreadsheetId: resolvedSheetId,
167
+ range: resolvedRange
168
+ });
169
+ const numRows = ((_a = response == null ? void 0 : response.data) == null ? void 0 : _a.values.length) ?? 0;
170
+ console.log(`${numRows} rows retrieved.`);
171
+ const nextState = { ...composeNextState(state, response.data), response };
172
+ return callback(nextState);
173
+ } catch (err) {
174
+ logError(err);
175
+ throw err;
176
+ }
177
+ };
178
+ }
88
179
 
89
180
  // src/index.js
90
181
  var src_default = Adaptor_exports;
91
182
  export {
92
183
  alterState,
93
184
  appendValues,
185
+ batchUpdateValues,
94
186
  combine,
95
187
  dataPath,
96
188
  dataValue,
@@ -100,6 +192,7 @@ export {
100
192
  field,
101
193
  fields,
102
194
  fn,
195
+ getValues,
103
196
  http,
104
197
  lastReferenceValue,
105
198
  merge,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-googlesheets",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "description": "A Google Sheets Language Pack for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -23,11 +23,10 @@
23
23
  "configuration-schema.json"
24
24
  ],
25
25
  "dependencies": {
26
- "@openfn/language-common": "^1.8.1",
26
+ "@openfn/language-common": "^1.12.0",
27
27
  "googleapis": "100.0.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@openfn/buildtools": "^1.0.2",
31
30
  "@openfn/simple-ast": "0.4.1",
32
31
  "assertion-error": "1.1.0",
33
32
  "chai": "4.3.6",
@@ -26,7 +26,50 @@ export function execute(...operations: Operations): Operation;
26
26
  * })
27
27
  * @function
28
28
  * @param {Object} params - Data object to add to the spreadsheet.
29
+ * @param {string} [params.spreadsheetId] The spreadsheet ID.
30
+ * @param {string} [params.range] The range of values to update.
31
+ * @param {array} [params.values] A 2d array of values to update.
29
32
  * @returns {Operation}
30
33
  */
31
- export function appendValues(params: any): Operation;
34
+ export function appendValues(params: {
35
+ spreadsheetId?: string;
36
+ range?: string;
37
+ values?: any[];
38
+ }, callback?: (s: any) => any): Operation;
39
+ /**
40
+ * Batch update values in a Spreadsheet.
41
+ * @example
42
+ * batchUpdateValues({
43
+ * spreadsheetId: '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',
44
+ * range: 'Sheet1!A1:E1',
45
+ * values: [
46
+ * ['From expression', '$15', '2', '3/15/2016'],
47
+ * ['Really now!', '$100', '1', '3/20/2016'],
48
+ * ],
49
+ * })
50
+ * @function
51
+ * @param {Object} params - Data object to add to the spreadsheet.
52
+ * @param {string} [params.spreadsheetId] The spreadsheet ID.
53
+ * @param {string} [params.range] The range of values to update.
54
+ * @param {string} [params.valueInputOption] (Optional) Value update options. Defaults to 'USER_ENTERED'
55
+ * @param {array} [params.values] A 2d array of values to update.
56
+ * @returns {Operation} spreadsheet information
57
+ */
58
+ export function batchUpdateValues(params: {
59
+ spreadsheetId?: string;
60
+ range?: string;
61
+ valueInputOption?: string;
62
+ values?: any[];
63
+ }, callback?: (s: any) => any): Operation;
64
+ /**
65
+ * Gets cell values from a Spreadsheet.
66
+ * @public
67
+ * @example
68
+ * getValues('1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos','Sheet1!A1:E1')
69
+ * @function
70
+ * @param {string} spreadsheetId The spreadsheet ID.
71
+ * @param {string} range The sheet range.
72
+ * @returns {Operation} spreadsheet information
73
+ */
74
+ export function getValues(spreadsheetId: string, range: string, callback?: (s: any) => any): Operation;
32
75
  export { alterState, combine, dataPath, dataValue, each, field, fields, fn, http, lastReferenceValue, merge, sourceValue } from "@openfn/language-common";