@openfn/language-googlesheets 4.1.2 → 5.1.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/README.md +47 -0
- package/ast.json +73 -80
- package/configuration-schema.json +16 -2
- package/dist/index.cjs +56 -40
- package/dist/index.js +56 -40
- package/package.json +4 -2
- package/types/Adaptor.d.ts +33 -39
package/README.md
CHANGED
|
@@ -12,6 +12,53 @@ official
|
|
|
12
12
|
[configuration-schema](https://docs.openfn.org/adaptors/packages/googlesheets-configuration-schema/)
|
|
13
13
|
definition.
|
|
14
14
|
|
|
15
|
+
This adaptor supports two authentication methods: **OAuth2** and **Service Account**.
|
|
16
|
+
|
|
17
|
+
#### OAuth2
|
|
18
|
+
|
|
19
|
+
**On app.openfn.org:** Click **Sign in with Google Sheets** in the credential setup form. Authentication is handled for you and no manual token management is needed.
|
|
20
|
+
|
|
21
|
+
**Using the OpenFn CLI locally:** Use the [gcloud CLI](https://cloud.google.com/sdk/docs/install) to print a temporary access token and provide it in your configuration:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
gcloud auth print-access-token
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"configuration": {
|
|
30
|
+
"access_token": "ya29.A0..."
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
#### Service Account
|
|
36
|
+
|
|
37
|
+
Service accounts are ideal for server-to-server integrations that run without
|
|
38
|
+
user interaction. Use a service account when you need reliable, long-running
|
|
39
|
+
access without managing OAuth2 token refresh.
|
|
40
|
+
|
|
41
|
+
To create a service account and obtain a JSON key file, follow the
|
|
42
|
+
[Google Cloud service account documentation](https://cloud.google.com/iam/docs/service-accounts-create).
|
|
43
|
+
Your OpenFn credential requires the `client_email` and `private_key` fields
|
|
44
|
+
from the downloaded JSON key file.
|
|
45
|
+
|
|
46
|
+
**Share the spreadsheet with the service account**
|
|
47
|
+
|
|
48
|
+
A service account does not have access to any spreadsheet by default. You must
|
|
49
|
+
share each spreadsheet the adaptor needs to access, just like you would share
|
|
50
|
+
with any other Google user.
|
|
51
|
+
|
|
52
|
+
1. Open the spreadsheet in Google Sheets.
|
|
53
|
+
2. Click **Share** (top-right corner).
|
|
54
|
+
3. Enter the service account's `client_email` address.
|
|
55
|
+
4. Grant at least **Editor** access (or **Viewer** if read-only is sufficient).
|
|
56
|
+
5. Click **Send**.
|
|
57
|
+
|
|
58
|
+
> **Note:** The service account email looks like
|
|
59
|
+
> `name@project-id.iam.gserviceaccount.com`. If you see a "This person may not
|
|
60
|
+
> be a Google user" warning, you can safely ignore it and proceed.
|
|
61
|
+
|
|
15
62
|
### appendValues()
|
|
16
63
|
|
|
17
64
|
Add rows to an existing sheet:
|
package/ast.json
CHANGED
|
@@ -3,11 +3,13 @@
|
|
|
3
3
|
{
|
|
4
4
|
"name": "appendValues",
|
|
5
5
|
"params": [
|
|
6
|
-
"
|
|
7
|
-
"
|
|
6
|
+
"spreadsheetId",
|
|
7
|
+
"range",
|
|
8
|
+
"values",
|
|
9
|
+
"options"
|
|
8
10
|
],
|
|
9
11
|
"docs": {
|
|
10
|
-
"description": "
|
|
12
|
+
"description": "Append one or more rows to a spreadsheet range.\nhttps://developers.google.com/sheets/api/samples/writing#append_values",
|
|
11
13
|
"tags": [
|
|
12
14
|
{
|
|
13
15
|
"title": "public",
|
|
@@ -16,7 +18,7 @@
|
|
|
16
18
|
},
|
|
17
19
|
{
|
|
18
20
|
"title": "example",
|
|
19
|
-
"description": "appendValues(
|
|
21
|
+
"description": "appendValues(\n '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',\n 'Sheet1!A1:E1',\n [['From expression', '$15', '2', '3/15/2016'], ['Really now!', '$100', '1', '3/20/2016']]\n)"
|
|
20
22
|
},
|
|
21
23
|
{
|
|
22
24
|
"title": "function",
|
|
@@ -25,57 +27,54 @@
|
|
|
25
27
|
},
|
|
26
28
|
{
|
|
27
29
|
"title": "param",
|
|
28
|
-
"description": "
|
|
30
|
+
"description": "The spreadsheet ID.",
|
|
29
31
|
"type": {
|
|
30
32
|
"type": "NameExpression",
|
|
31
|
-
"name": "
|
|
33
|
+
"name": "string"
|
|
32
34
|
},
|
|
33
|
-
"name": "
|
|
35
|
+
"name": "spreadsheetId"
|
|
34
36
|
},
|
|
35
37
|
{
|
|
36
38
|
"title": "param",
|
|
37
|
-
"description": "The
|
|
39
|
+
"description": "The sheet range.",
|
|
38
40
|
"type": {
|
|
39
|
-
"type": "
|
|
40
|
-
"
|
|
41
|
-
"type": "NameExpression",
|
|
42
|
-
"name": "string"
|
|
43
|
-
}
|
|
41
|
+
"type": "NameExpression",
|
|
42
|
+
"name": "string"
|
|
44
43
|
},
|
|
45
|
-
"name": "
|
|
44
|
+
"name": "range"
|
|
46
45
|
},
|
|
47
46
|
{
|
|
48
47
|
"title": "param",
|
|
49
|
-
"description": "The
|
|
48
|
+
"description": "The values to append.",
|
|
50
49
|
"type": {
|
|
51
|
-
"type": "
|
|
52
|
-
"
|
|
53
|
-
"type": "NameExpression",
|
|
54
|
-
"name": "string"
|
|
55
|
-
}
|
|
50
|
+
"type": "NameExpression",
|
|
51
|
+
"name": "array"
|
|
56
52
|
},
|
|
57
|
-
"name": "
|
|
53
|
+
"name": "values"
|
|
58
54
|
},
|
|
59
55
|
{
|
|
60
56
|
"title": "param",
|
|
61
|
-
"description": "
|
|
57
|
+
"description": "Optional settings.",
|
|
62
58
|
"type": {
|
|
63
59
|
"type": "OptionalType",
|
|
64
60
|
"expression": {
|
|
65
61
|
"type": "NameExpression",
|
|
66
|
-
"name": "
|
|
62
|
+
"name": "Object"
|
|
67
63
|
}
|
|
68
64
|
},
|
|
69
|
-
"name": "
|
|
65
|
+
"name": "options"
|
|
70
66
|
},
|
|
71
67
|
{
|
|
72
68
|
"title": "param",
|
|
73
|
-
"description": "
|
|
69
|
+
"description": "Defaults to 'USER_ENTERED'.",
|
|
74
70
|
"type": {
|
|
75
|
-
"type": "
|
|
76
|
-
"
|
|
71
|
+
"type": "OptionalType",
|
|
72
|
+
"expression": {
|
|
73
|
+
"type": "NameExpression",
|
|
74
|
+
"name": "string"
|
|
75
|
+
}
|
|
77
76
|
},
|
|
78
|
-
"name": "
|
|
77
|
+
"name": "options.valueInputOption"
|
|
79
78
|
},
|
|
80
79
|
{
|
|
81
80
|
"title": "returns",
|
|
@@ -92,15 +91,17 @@
|
|
|
92
91
|
{
|
|
93
92
|
"name": "batchUpdateValues",
|
|
94
93
|
"params": [
|
|
95
|
-
"
|
|
96
|
-
"
|
|
94
|
+
"spreadsheetId",
|
|
95
|
+
"data",
|
|
96
|
+
"options"
|
|
97
97
|
],
|
|
98
98
|
"docs": {
|
|
99
99
|
"description": "Batch update values in a Spreadsheet.",
|
|
100
100
|
"tags": [
|
|
101
101
|
{
|
|
102
102
|
"title": "example",
|
|
103
|
-
"description": "batchUpdateValues(
|
|
103
|
+
"description": "batchUpdateValues(\n '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',\n [\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)",
|
|
104
|
+
"caption": "Update multiple separate ranges"
|
|
104
105
|
},
|
|
105
106
|
{
|
|
106
107
|
"title": "function",
|
|
@@ -114,40 +115,63 @@
|
|
|
114
115
|
},
|
|
115
116
|
{
|
|
116
117
|
"title": "param",
|
|
117
|
-
"description": "
|
|
118
|
+
"description": "The spreadsheet ID.",
|
|
118
119
|
"type": {
|
|
119
120
|
"type": "NameExpression",
|
|
120
|
-
"name": "
|
|
121
|
+
"name": "string"
|
|
121
122
|
},
|
|
122
|
-
"name": "
|
|
123
|
+
"name": "spreadsheetId"
|
|
123
124
|
},
|
|
124
125
|
{
|
|
125
126
|
"title": "param",
|
|
126
|
-
"description": "
|
|
127
|
+
"description": "Array of range/values objects to update.",
|
|
127
128
|
"type": {
|
|
128
|
-
"type": "
|
|
129
|
+
"type": "TypeApplication",
|
|
129
130
|
"expression": {
|
|
130
131
|
"type": "NameExpression",
|
|
131
|
-
"name": "
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
132
|
+
"name": "Array"
|
|
133
|
+
},
|
|
134
|
+
"applications": [
|
|
135
|
+
{
|
|
136
|
+
"type": "RecordType",
|
|
137
|
+
"fields": [
|
|
138
|
+
{
|
|
139
|
+
"type": "FieldType",
|
|
140
|
+
"key": "range",
|
|
141
|
+
"value": {
|
|
142
|
+
"type": "NameExpression",
|
|
143
|
+
"name": "string"
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
"type": "FieldType",
|
|
148
|
+
"key": "values",
|
|
149
|
+
"value": {
|
|
150
|
+
"type": "NameExpression",
|
|
151
|
+
"name": "array"
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
}
|
|
156
|
+
]
|
|
157
|
+
},
|
|
158
|
+
"name": "data"
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
"title": "param",
|
|
162
|
+
"description": "Optional settings.",
|
|
139
163
|
"type": {
|
|
140
164
|
"type": "OptionalType",
|
|
141
165
|
"expression": {
|
|
142
166
|
"type": "NameExpression",
|
|
143
|
-
"name": "
|
|
167
|
+
"name": "Object"
|
|
144
168
|
}
|
|
145
169
|
},
|
|
146
|
-
"name": "
|
|
170
|
+
"name": "options"
|
|
147
171
|
},
|
|
148
172
|
{
|
|
149
173
|
"title": "param",
|
|
150
|
-
"description": "
|
|
174
|
+
"description": "Defaults to 'USER_ENTERED'.",
|
|
151
175
|
"type": {
|
|
152
176
|
"type": "OptionalType",
|
|
153
177
|
"expression": {
|
|
@@ -155,28 +179,7 @@
|
|
|
155
179
|
"name": "string"
|
|
156
180
|
}
|
|
157
181
|
},
|
|
158
|
-
"name": "
|
|
159
|
-
},
|
|
160
|
-
{
|
|
161
|
-
"title": "param",
|
|
162
|
-
"description": "A 2d array of values to update.",
|
|
163
|
-
"type": {
|
|
164
|
-
"type": "OptionalType",
|
|
165
|
-
"expression": {
|
|
166
|
-
"type": "NameExpression",
|
|
167
|
-
"name": "array"
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
"name": "params.values"
|
|
171
|
-
},
|
|
172
|
-
{
|
|
173
|
-
"title": "param",
|
|
174
|
-
"description": "(Optional) callback function",
|
|
175
|
-
"type": {
|
|
176
|
-
"type": "NameExpression",
|
|
177
|
-
"name": "function"
|
|
178
|
-
},
|
|
179
|
-
"name": "callback"
|
|
182
|
+
"name": "options.valueInputOption"
|
|
180
183
|
},
|
|
181
184
|
{
|
|
182
185
|
"title": "returns",
|
|
@@ -194,8 +197,7 @@
|
|
|
194
197
|
"name": "getValues",
|
|
195
198
|
"params": [
|
|
196
199
|
"spreadsheetId",
|
|
197
|
-
"range"
|
|
198
|
-
"callback"
|
|
200
|
+
"range"
|
|
199
201
|
],
|
|
200
202
|
"docs": {
|
|
201
203
|
"description": "Gets cell values from a Spreadsheet.",
|
|
@@ -232,15 +234,6 @@
|
|
|
232
234
|
},
|
|
233
235
|
"name": "range"
|
|
234
236
|
},
|
|
235
|
-
{
|
|
236
|
-
"title": "param",
|
|
237
|
-
"description": "(Optional) callback function",
|
|
238
|
-
"type": {
|
|
239
|
-
"type": "NameExpression",
|
|
240
|
-
"name": "function"
|
|
241
|
-
},
|
|
242
|
-
"name": "callback"
|
|
243
|
-
},
|
|
244
237
|
{
|
|
245
238
|
"title": "returns",
|
|
246
239
|
"description": "spreadsheet information",
|
|
@@ -11,9 +11,23 @@
|
|
|
11
11
|
"examples": [
|
|
12
12
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjlGWERwYmZNRlQyU3ZRdVhoODQ2WVR3RUlCdyIsI"
|
|
13
13
|
]
|
|
14
|
+
},
|
|
15
|
+
"private_key": {
|
|
16
|
+
"title": "Private Key",
|
|
17
|
+
"type": "string",
|
|
18
|
+
"description": "RSA private key from the Google service account JSON key file. Required for service accounts",
|
|
19
|
+
"writeOnly": true,
|
|
20
|
+
"minLength": 1,
|
|
21
|
+
"examples": ["-----BEGIN RSA PRIVATE KEY-----\nMIIEow..."]
|
|
22
|
+
},
|
|
23
|
+
"client_email": {
|
|
24
|
+
"title": "Client Email",
|
|
25
|
+
"type": "string",
|
|
26
|
+
"description": "Service account email address (e.g. my-app@project-id.iam.gserviceaccount.com). Required for service accounts",
|
|
27
|
+
"minLength": 1,
|
|
28
|
+
"examples": ["my-app@project-id.iam.gserviceaccount.com"]
|
|
14
29
|
}
|
|
15
30
|
},
|
|
16
31
|
"type": "object",
|
|
17
|
-
"additionalProperties": true
|
|
18
|
-
"required": ["access_token"]
|
|
32
|
+
"additionalProperties": true
|
|
19
33
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -68,10 +68,31 @@ var import_util = require("@openfn/language-common/util");
|
|
|
68
68
|
var import_googleapis = require("googleapis");
|
|
69
69
|
var import_language_common2 = require("@openfn/language-common");
|
|
70
70
|
var client = void 0;
|
|
71
|
-
function createConnection(state) {
|
|
72
|
-
const {
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
async function createConnection(state) {
|
|
72
|
+
const {
|
|
73
|
+
accessToken,
|
|
74
|
+
private_key,
|
|
75
|
+
client_email,
|
|
76
|
+
scopes = []
|
|
77
|
+
} = state.configuration;
|
|
78
|
+
const mandatoryScopes = [
|
|
79
|
+
"https://www.googleapis.com/auth/spreadsheets",
|
|
80
|
+
"https://www.googleapis.com/auth/userinfo.email",
|
|
81
|
+
"https://www.googleapis.com/auth/userinfo.profile",
|
|
82
|
+
"openid"
|
|
83
|
+
];
|
|
84
|
+
let auth;
|
|
85
|
+
if (private_key && client_email) {
|
|
86
|
+
auth = new import_googleapis.google.auth.JWT({
|
|
87
|
+
email: client_email,
|
|
88
|
+
key: private_key,
|
|
89
|
+
scopes: [...mandatoryScopes, ...scopes]
|
|
90
|
+
});
|
|
91
|
+
await auth.authorize();
|
|
92
|
+
} else {
|
|
93
|
+
auth = new import_googleapis.google.auth.OAuth2();
|
|
94
|
+
auth.credentials = { access_token: accessToken };
|
|
95
|
+
}
|
|
75
96
|
client = import_googleapis.google.sheets({ version: "v4", auth });
|
|
76
97
|
return state;
|
|
77
98
|
}
|
|
@@ -96,6 +117,8 @@ function execute(...operations) {
|
|
|
96
117
|
data: null
|
|
97
118
|
};
|
|
98
119
|
return (state) => {
|
|
120
|
+
var _a, _b;
|
|
121
|
+
const isServiceAccount = ((_a = state.configuration) == null ? void 0 : _a.private_key) && ((_b = state.configuration) == null ? void 0 : _b.client_email);
|
|
99
122
|
return (0, import_language_common.execute)(
|
|
100
123
|
createConnection,
|
|
101
124
|
...operations,
|
|
@@ -103,28 +126,33 @@ function execute(...operations) {
|
|
|
103
126
|
)({
|
|
104
127
|
...initialState,
|
|
105
128
|
...state,
|
|
106
|
-
configuration: (0, import_util.normalizeOauthConfig)(state.configuration)
|
|
129
|
+
configuration: isServiceAccount ? state.configuration : (0, import_util.normalizeOauthConfig)(state.configuration)
|
|
107
130
|
});
|
|
108
131
|
};
|
|
109
132
|
}
|
|
110
|
-
function appendValues(
|
|
133
|
+
function appendValues(spreadsheetId, range, values, options = {}) {
|
|
111
134
|
return (state) => {
|
|
112
|
-
const [
|
|
113
|
-
|
|
114
|
-
|
|
135
|
+
const [
|
|
136
|
+
resolvedSpreadsheetId,
|
|
137
|
+
resolvedRange,
|
|
138
|
+
resolvedValues,
|
|
139
|
+
resolvedOptions
|
|
140
|
+
] = (0, import_util.expandReferences)(state, spreadsheetId, range, values, options);
|
|
141
|
+
const { valueInputOption = "USER_ENTERED" } = resolvedOptions;
|
|
142
|
+
if (!resolvedValues || resolvedValues.length === 0) {
|
|
115
143
|
console.log("Warning: empty values array");
|
|
116
144
|
return state;
|
|
117
145
|
}
|
|
118
146
|
return new Promise((resolve, reject) => {
|
|
119
147
|
client.spreadsheets.values.append(
|
|
120
148
|
{
|
|
121
|
-
spreadsheetId,
|
|
122
|
-
range,
|
|
123
|
-
valueInputOption
|
|
149
|
+
spreadsheetId: resolvedSpreadsheetId,
|
|
150
|
+
range: resolvedRange,
|
|
151
|
+
valueInputOption,
|
|
124
152
|
resource: {
|
|
125
|
-
range,
|
|
153
|
+
range: resolvedRange,
|
|
126
154
|
majorDimension: "ROWS",
|
|
127
|
-
values
|
|
155
|
+
values: resolvedValues
|
|
128
156
|
}
|
|
129
157
|
},
|
|
130
158
|
function(err, response) {
|
|
@@ -134,54 +162,42 @@ function appendValues(params, callback = (s) => s) {
|
|
|
134
162
|
} else {
|
|
135
163
|
console.log("Success! Here is the response from Google:");
|
|
136
164
|
console.log(response.data);
|
|
137
|
-
resolve(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
})
|
|
142
|
-
);
|
|
165
|
+
resolve({
|
|
166
|
+
...(0, import_language_common.composeNextState)(state, response.data),
|
|
167
|
+
response
|
|
168
|
+
});
|
|
143
169
|
}
|
|
144
170
|
}
|
|
145
171
|
);
|
|
146
172
|
});
|
|
147
173
|
};
|
|
148
174
|
}
|
|
149
|
-
function batchUpdateValues(
|
|
175
|
+
function batchUpdateValues(spreadsheetId, data, options = {}) {
|
|
150
176
|
return async (state) => {
|
|
151
|
-
const [
|
|
152
|
-
const {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
valueInputOption = "USER_ENTERED",
|
|
156
|
-
values
|
|
157
|
-
} = resolvedParams;
|
|
158
|
-
if (!values || values.length === 0) {
|
|
159
|
-
console.log("Warning: empty values array");
|
|
177
|
+
const [resolvedSpreadsheetId, resolvedData, resolvedOptions] = (0, import_util.expandReferences)(state, spreadsheetId, data, options);
|
|
178
|
+
const { valueInputOption = "USER_ENTERED" } = resolvedOptions;
|
|
179
|
+
if (!resolvedData || resolvedData.length === 0) {
|
|
180
|
+
console.log("Warning: empty data array");
|
|
160
181
|
return state;
|
|
161
182
|
}
|
|
162
183
|
const resource = {
|
|
163
|
-
data:
|
|
164
|
-
{
|
|
165
|
-
range,
|
|
166
|
-
values
|
|
167
|
-
}
|
|
168
|
-
],
|
|
184
|
+
data: resolvedData,
|
|
169
185
|
valueInputOption
|
|
170
186
|
};
|
|
171
187
|
try {
|
|
172
188
|
const response = await client.spreadsheets.values.batchUpdate({
|
|
173
|
-
spreadsheetId,
|
|
189
|
+
spreadsheetId: resolvedSpreadsheetId,
|
|
174
190
|
resource
|
|
175
191
|
});
|
|
176
192
|
console.log("%d cells updated.", response.data.totalUpdatedCells);
|
|
177
|
-
return
|
|
193
|
+
return { ...(0, import_language_common.composeNextState)(state, response.data), response };
|
|
178
194
|
} catch (err) {
|
|
179
195
|
logError(err);
|
|
180
196
|
throw err;
|
|
181
197
|
}
|
|
182
198
|
};
|
|
183
199
|
}
|
|
184
|
-
function getValues(spreadsheetId, range
|
|
200
|
+
function getValues(spreadsheetId, range) {
|
|
185
201
|
return async (state) => {
|
|
186
202
|
var _a;
|
|
187
203
|
const [resolvedSheetId, resolvedRange] = (0, import_util.expandReferences)(
|
|
@@ -197,7 +213,7 @@ function getValues(spreadsheetId, range, callback = (s) => s) {
|
|
|
197
213
|
const numRows = ((_a = response == null ? void 0 : response.data) == null ? void 0 : _a.values.length) ?? 0;
|
|
198
214
|
console.log(`${numRows} rows retrieved.`);
|
|
199
215
|
const nextState = { ...(0, import_language_common.composeNextState)(state, response.data), response };
|
|
200
|
-
return
|
|
216
|
+
return nextState;
|
|
201
217
|
} catch (err) {
|
|
202
218
|
logError(err);
|
|
203
219
|
throw err;
|
package/dist/index.js
CHANGED
|
@@ -52,10 +52,31 @@ import {
|
|
|
52
52
|
sourceValue
|
|
53
53
|
} from "@openfn/language-common";
|
|
54
54
|
var client = void 0;
|
|
55
|
-
function createConnection(state) {
|
|
56
|
-
const {
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
async function createConnection(state) {
|
|
56
|
+
const {
|
|
57
|
+
accessToken,
|
|
58
|
+
private_key,
|
|
59
|
+
client_email,
|
|
60
|
+
scopes = []
|
|
61
|
+
} = state.configuration;
|
|
62
|
+
const mandatoryScopes = [
|
|
63
|
+
"https://www.googleapis.com/auth/spreadsheets",
|
|
64
|
+
"https://www.googleapis.com/auth/userinfo.email",
|
|
65
|
+
"https://www.googleapis.com/auth/userinfo.profile",
|
|
66
|
+
"openid"
|
|
67
|
+
];
|
|
68
|
+
let auth;
|
|
69
|
+
if (private_key && client_email) {
|
|
70
|
+
auth = new google.auth.JWT({
|
|
71
|
+
email: client_email,
|
|
72
|
+
key: private_key,
|
|
73
|
+
scopes: [...mandatoryScopes, ...scopes]
|
|
74
|
+
});
|
|
75
|
+
await auth.authorize();
|
|
76
|
+
} else {
|
|
77
|
+
auth = new google.auth.OAuth2();
|
|
78
|
+
auth.credentials = { access_token: accessToken };
|
|
79
|
+
}
|
|
59
80
|
client = google.sheets({ version: "v4", auth });
|
|
60
81
|
return state;
|
|
61
82
|
}
|
|
@@ -80,6 +101,8 @@ function execute(...operations) {
|
|
|
80
101
|
data: null
|
|
81
102
|
};
|
|
82
103
|
return (state) => {
|
|
104
|
+
var _a, _b;
|
|
105
|
+
const isServiceAccount = ((_a = state.configuration) == null ? void 0 : _a.private_key) && ((_b = state.configuration) == null ? void 0 : _b.client_email);
|
|
83
106
|
return commonExecute(
|
|
84
107
|
createConnection,
|
|
85
108
|
...operations,
|
|
@@ -87,28 +110,33 @@ function execute(...operations) {
|
|
|
87
110
|
)({
|
|
88
111
|
...initialState,
|
|
89
112
|
...state,
|
|
90
|
-
configuration: normalizeOauthConfig(state.configuration)
|
|
113
|
+
configuration: isServiceAccount ? state.configuration : normalizeOauthConfig(state.configuration)
|
|
91
114
|
});
|
|
92
115
|
};
|
|
93
116
|
}
|
|
94
|
-
function appendValues(
|
|
117
|
+
function appendValues(spreadsheetId, range, values, options = {}) {
|
|
95
118
|
return (state) => {
|
|
96
|
-
const [
|
|
97
|
-
|
|
98
|
-
|
|
119
|
+
const [
|
|
120
|
+
resolvedSpreadsheetId,
|
|
121
|
+
resolvedRange,
|
|
122
|
+
resolvedValues,
|
|
123
|
+
resolvedOptions
|
|
124
|
+
] = expandReferences(state, spreadsheetId, range, values, options);
|
|
125
|
+
const { valueInputOption = "USER_ENTERED" } = resolvedOptions;
|
|
126
|
+
if (!resolvedValues || resolvedValues.length === 0) {
|
|
99
127
|
console.log("Warning: empty values array");
|
|
100
128
|
return state;
|
|
101
129
|
}
|
|
102
130
|
return new Promise((resolve, reject) => {
|
|
103
131
|
client.spreadsheets.values.append(
|
|
104
132
|
{
|
|
105
|
-
spreadsheetId,
|
|
106
|
-
range,
|
|
107
|
-
valueInputOption
|
|
133
|
+
spreadsheetId: resolvedSpreadsheetId,
|
|
134
|
+
range: resolvedRange,
|
|
135
|
+
valueInputOption,
|
|
108
136
|
resource: {
|
|
109
|
-
range,
|
|
137
|
+
range: resolvedRange,
|
|
110
138
|
majorDimension: "ROWS",
|
|
111
|
-
values
|
|
139
|
+
values: resolvedValues
|
|
112
140
|
}
|
|
113
141
|
},
|
|
114
142
|
function(err, response) {
|
|
@@ -118,54 +146,42 @@ function appendValues(params, callback = (s) => s) {
|
|
|
118
146
|
} else {
|
|
119
147
|
console.log("Success! Here is the response from Google:");
|
|
120
148
|
console.log(response.data);
|
|
121
|
-
resolve(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
})
|
|
126
|
-
);
|
|
149
|
+
resolve({
|
|
150
|
+
...composeNextState(state, response.data),
|
|
151
|
+
response
|
|
152
|
+
});
|
|
127
153
|
}
|
|
128
154
|
}
|
|
129
155
|
);
|
|
130
156
|
});
|
|
131
157
|
};
|
|
132
158
|
}
|
|
133
|
-
function batchUpdateValues(
|
|
159
|
+
function batchUpdateValues(spreadsheetId, data, options = {}) {
|
|
134
160
|
return async (state) => {
|
|
135
|
-
const [
|
|
136
|
-
const {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
valueInputOption = "USER_ENTERED",
|
|
140
|
-
values
|
|
141
|
-
} = resolvedParams;
|
|
142
|
-
if (!values || values.length === 0) {
|
|
143
|
-
console.log("Warning: empty values array");
|
|
161
|
+
const [resolvedSpreadsheetId, resolvedData, resolvedOptions] = expandReferences(state, spreadsheetId, data, options);
|
|
162
|
+
const { valueInputOption = "USER_ENTERED" } = resolvedOptions;
|
|
163
|
+
if (!resolvedData || resolvedData.length === 0) {
|
|
164
|
+
console.log("Warning: empty data array");
|
|
144
165
|
return state;
|
|
145
166
|
}
|
|
146
167
|
const resource = {
|
|
147
|
-
data:
|
|
148
|
-
{
|
|
149
|
-
range,
|
|
150
|
-
values
|
|
151
|
-
}
|
|
152
|
-
],
|
|
168
|
+
data: resolvedData,
|
|
153
169
|
valueInputOption
|
|
154
170
|
};
|
|
155
171
|
try {
|
|
156
172
|
const response = await client.spreadsheets.values.batchUpdate({
|
|
157
|
-
spreadsheetId,
|
|
173
|
+
spreadsheetId: resolvedSpreadsheetId,
|
|
158
174
|
resource
|
|
159
175
|
});
|
|
160
176
|
console.log("%d cells updated.", response.data.totalUpdatedCells);
|
|
161
|
-
return
|
|
177
|
+
return { ...composeNextState(state, response.data), response };
|
|
162
178
|
} catch (err) {
|
|
163
179
|
logError(err);
|
|
164
180
|
throw err;
|
|
165
181
|
}
|
|
166
182
|
};
|
|
167
183
|
}
|
|
168
|
-
function getValues(spreadsheetId, range
|
|
184
|
+
function getValues(spreadsheetId, range) {
|
|
169
185
|
return async (state) => {
|
|
170
186
|
var _a;
|
|
171
187
|
const [resolvedSheetId, resolvedRange] = expandReferences(
|
|
@@ -181,7 +197,7 @@ function getValues(spreadsheetId, range, callback = (s) => s) {
|
|
|
181
197
|
const numRows = ((_a = response == null ? void 0 : response.data) == null ? void 0 : _a.values.length) ?? 0;
|
|
182
198
|
console.log(`${numRows} rows retrieved.`);
|
|
183
199
|
const nextState = { ...composeNextState(state, response.data), response };
|
|
184
|
-
return
|
|
200
|
+
return nextState;
|
|
185
201
|
} catch (err) {
|
|
186
202
|
logError(err);
|
|
187
203
|
throw err;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfn/language-googlesheets",
|
|
3
3
|
"label": "Google Sheets",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.1.0",
|
|
5
5
|
"description": "A Google Sheets Language Pack for OpenFn",
|
|
6
6
|
"homepage": "https://docs.openfn.org",
|
|
7
7
|
"repository": {
|
|
@@ -32,8 +32,10 @@
|
|
|
32
32
|
"assertion-error": "1.1.0",
|
|
33
33
|
"chai": "4.3.6",
|
|
34
34
|
"deep-eql": "4.1.1",
|
|
35
|
+
"mocha": "^10.7.3",
|
|
35
36
|
"nock": "13.2.9",
|
|
36
|
-
"rimraf": "3.0.2"
|
|
37
|
+
"rimraf": "3.0.2",
|
|
38
|
+
"sinon": "^21.1.2"
|
|
37
39
|
},
|
|
38
40
|
"type": "module",
|
|
39
41
|
"types": "types/index.d.ts",
|
package/types/Adaptor.d.ts
CHANGED
|
@@ -12,58 +12,53 @@
|
|
|
12
12
|
*/
|
|
13
13
|
export function execute(...operations: Operations): Operation;
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* Append one or more rows to a spreadsheet range.
|
|
16
16
|
* https://developers.google.com/sheets/api/samples/writing#append_values
|
|
17
17
|
* @public
|
|
18
18
|
* @example
|
|
19
|
-
* appendValues(
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* ['Really now!', '$100', '1', '3/20/2016'],
|
|
25
|
-
* ],
|
|
26
|
-
* })
|
|
19
|
+
* appendValues(
|
|
20
|
+
* '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',
|
|
21
|
+
* 'Sheet1!A1:E1',
|
|
22
|
+
* [['From expression', '$15', '2', '3/15/2016'], ['Really now!', '$100', '1', '3/20/2016']]
|
|
23
|
+
* )
|
|
27
24
|
* @function
|
|
28
|
-
* @param {
|
|
29
|
-
* @param {string}
|
|
30
|
-
* @param {
|
|
31
|
-
* @param {
|
|
32
|
-
* @param {
|
|
25
|
+
* @param {string} spreadsheetId - The spreadsheet ID.
|
|
26
|
+
* @param {string} range - The sheet range.
|
|
27
|
+
* @param {array} values - The values to append.
|
|
28
|
+
* @param {Object} [options] - Optional settings.
|
|
29
|
+
* @param {string} [options.valueInputOption] - Defaults to 'USER_ENTERED'.
|
|
33
30
|
* @returns {Operation}
|
|
34
31
|
*/
|
|
35
|
-
export function appendValues(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
values?: any[];
|
|
39
|
-
}, callback?: Function): Operation;
|
|
32
|
+
export function appendValues(spreadsheetId: string, range: string, values: any[], options?: {
|
|
33
|
+
valueInputOption?: string;
|
|
34
|
+
}): Operation;
|
|
40
35
|
/**
|
|
41
36
|
* Batch update values in a Spreadsheet.
|
|
42
37
|
* @example
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
38
|
+
* <caption>Update multiple separate ranges</caption>
|
|
39
|
+
* batchUpdateValues(
|
|
40
|
+
* '1O-a4_RgPF_p8W3I6b5M9wobA3-CBW8hLClZfUik5sos',
|
|
41
|
+
* [
|
|
42
|
+
* { range: 'Sheet1!A1', values: [['value1']] },
|
|
43
|
+
* { range: 'Sheet1!B5', values: [['value2']] },
|
|
44
|
+
* { range: 'Sheet1!D10:E11', values: [['a', 'b'], ['c', 'd']] },
|
|
49
45
|
* ],
|
|
50
|
-
* }
|
|
46
|
+
* { valueInputOption: 'RAW' }
|
|
47
|
+
* )
|
|
51
48
|
* @function
|
|
52
49
|
* @public
|
|
53
|
-
* @param {
|
|
54
|
-
* @param {string}
|
|
55
|
-
* @param {
|
|
56
|
-
* @param {string} [
|
|
57
|
-
* @param {array} [params.values] A 2d array of values to update.
|
|
58
|
-
* @param {function} callback - (Optional) callback function
|
|
50
|
+
* @param {string} spreadsheetId - The spreadsheet ID.
|
|
51
|
+
* @param {Array<{range: string, values: array}>} data - Array of range/values objects to update.
|
|
52
|
+
* @param {Object} [options] - Optional settings.
|
|
53
|
+
* @param {string} [options.valueInputOption] - Defaults to 'USER_ENTERED'.
|
|
59
54
|
* @returns {Operation} spreadsheet information
|
|
60
55
|
*/
|
|
61
|
-
export function batchUpdateValues(
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
export function batchUpdateValues(spreadsheetId: string, data: Array<{
|
|
57
|
+
range: string;
|
|
58
|
+
values: any[];
|
|
59
|
+
}>, options?: {
|
|
64
60
|
valueInputOption?: string;
|
|
65
|
-
|
|
66
|
-
}, callback?: Function): Operation;
|
|
61
|
+
}): Operation;
|
|
67
62
|
/**
|
|
68
63
|
* Gets cell values from a Spreadsheet.
|
|
69
64
|
* @public
|
|
@@ -72,8 +67,7 @@ export function batchUpdateValues(params: {
|
|
|
72
67
|
* @function
|
|
73
68
|
* @param {string} spreadsheetId The spreadsheet ID.
|
|
74
69
|
* @param {string} range The sheet range.
|
|
75
|
-
* @param {function} callback - (Optional) callback function
|
|
76
70
|
* @returns {Operation} spreadsheet information
|
|
77
71
|
*/
|
|
78
|
-
export function getValues(spreadsheetId: string, range: string
|
|
72
|
+
export function getValues(spreadsheetId: string, range: string): Operation;
|
|
79
73
|
export { alterState, combine, cursor, dataPath, dataValue, each, field, fields, fn, fnIf, lastReferenceValue, log, merge, sourceValue } from "@openfn/language-common";
|