@sheet2db/sdk 1.0.0 → 1.0.2
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 +329 -0
- package/package.json +2 -2
package/README.md
ADDED
@@ -0,0 +1,329 @@
|
|
1
|
+
# Sheet2DB Documentation
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
|
5
|
+
`Sheet2DB` is a TypeScript class that provides an easy interface to interact with Google Sheets via the [`sheet2db`](https://sheet2db.com) API. This class abstracts the complexity of API calls and provides a simple way to read, write, update, and delete data in Google Sheets. It supports both browser and Node.js environments.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```bash
|
10
|
+
npm install sheet2db
|
11
|
+
```
|
12
|
+
|
13
|
+
## Configuration Options
|
14
|
+
|
15
|
+
### Sheet2DBOptions
|
16
|
+
|
17
|
+
- **mode**: `'apikey' | 'connectionId'` - Authentication mode.
|
18
|
+
- **apiKey**: `string` (required for `'apikey'` mode) - API key for accessing the spreadsheet.
|
19
|
+
- **spreadsheetId**: `string` (required for `'apikey'` mode) - The ID of the Google spreadsheet.
|
20
|
+
- **version**: `"v1"` - API version, always "v1".
|
21
|
+
- **connectionId**: `string` (required for `'connectionId'` mode) - Connection ID for accessing the spreadsheet.
|
22
|
+
- **basicAuth**: `{ username: string; password: string; }` (optional for `'connectionId'` mode) - Basic authentication credentials.
|
23
|
+
- **jwtAuth**: `{ bearerToken: string; }` (optional for `'connectionId'` mode) - JWT authentication token.
|
24
|
+
- **fetchFn**: `typeof fetch` (optional) - Custom fetch function.
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### Import and Initialize
|
29
|
+
|
30
|
+
```typescript
|
31
|
+
import Sheet2DB, { Sheet2DBOptions } from 'sheet2db';
|
32
|
+
|
33
|
+
const options: Sheet2DBOptions = {
|
34
|
+
mode: 'apikey',
|
35
|
+
apiKey: 'your-api-key',
|
36
|
+
spreadsheetId: 'your-spreadsheet-id',
|
37
|
+
version: 'v1'
|
38
|
+
};
|
39
|
+
|
40
|
+
const options: Sheet2DBOptions = {
|
41
|
+
mode: 'connectionId',
|
42
|
+
connectionId: 'your-connection-id',
|
43
|
+
version: 'v1'
|
44
|
+
};
|
45
|
+
|
46
|
+
const sheet2db = new Sheet2DB(options);
|
47
|
+
```
|
48
|
+
|
49
|
+
## Methods
|
50
|
+
|
51
|
+
### ReadContent
|
52
|
+
|
53
|
+
Fetches content from the spreadsheet.
|
54
|
+
|
55
|
+
#### Parameters
|
56
|
+
|
57
|
+
- **limit** (optional): `number` - Maximum number of records to return.
|
58
|
+
- **offset** (optional): `number` - Number of records to skip before starting to return records.
|
59
|
+
- **sheet** (optional): `string` - The name of the sheet to read from.
|
60
|
+
- **format** (optional): `'records' | 'dict' | 'series' | 'split' | 'index' | 'raw'` - The format of the returned data.
|
61
|
+
- **cast_numbers** (optional): `string` - Cast numbers to specified type.
|
62
|
+
- **value_render** (optional): `'FORMATTED_VALUE' | 'UNFORMATTED_VALUE' | 'FORMULA'` - The value render option.
|
63
|
+
|
64
|
+
#### Example
|
65
|
+
|
66
|
+
```typescript
|
67
|
+
sheet2db.ReadContent({ sheet: 'Sheet1', limit: 10 })
|
68
|
+
.then(data => console.log(data))
|
69
|
+
.catch(error => console.error(error));
|
70
|
+
```
|
71
|
+
|
72
|
+
### Keys
|
73
|
+
|
74
|
+
Gets the keys of the spreadsheet.
|
75
|
+
|
76
|
+
#### Parameters
|
77
|
+
|
78
|
+
- **sheet** (optional): `string` - The name of the sheet to get keys from.
|
79
|
+
|
80
|
+
#### Example
|
81
|
+
|
82
|
+
```typescript
|
83
|
+
sheet2db.Keys({ sheet: 'Sheet1' })
|
84
|
+
.then(keys => console.log(keys))
|
85
|
+
.catch(error => console.error(error));
|
86
|
+
```
|
87
|
+
|
88
|
+
### Count
|
89
|
+
|
90
|
+
Gets the count of rows in the spreadsheet.
|
91
|
+
|
92
|
+
#### Parameters
|
93
|
+
|
94
|
+
- **sheet** (optional): `string` - The name of the sheet to count rows in.
|
95
|
+
|
96
|
+
#### Example
|
97
|
+
|
98
|
+
```typescript
|
99
|
+
sheet2db.Count({ sheet: 'Sheet1' })
|
100
|
+
.then(count => console.log(count))
|
101
|
+
.catch(error => console.error(error));
|
102
|
+
```
|
103
|
+
|
104
|
+
### Title
|
105
|
+
|
106
|
+
Gets the title of the spreadsheet.
|
107
|
+
|
108
|
+
#### Example
|
109
|
+
|
110
|
+
```typescript
|
111
|
+
sheet2db.Title()
|
112
|
+
.then(title => console.log(title))
|
113
|
+
.catch(error => console.error(error));
|
114
|
+
```
|
115
|
+
|
116
|
+
### Range
|
117
|
+
|
118
|
+
Gets a range of values from the spreadsheet.
|
119
|
+
|
120
|
+
#### Parameters
|
121
|
+
|
122
|
+
- **range**: `string` - The range to fetch, in A1 notation.
|
123
|
+
|
124
|
+
#### Example
|
125
|
+
|
126
|
+
```typescript
|
127
|
+
sheet2db.Range({ range: 'A1:B2' })
|
128
|
+
.then(range => console.log(range))
|
129
|
+
.catch(error => console.error(error));
|
130
|
+
```
|
131
|
+
|
132
|
+
### Search
|
133
|
+
|
134
|
+
Searches for records in the spreadsheet.
|
135
|
+
|
136
|
+
#### Parameters
|
137
|
+
|
138
|
+
- **sheet** (optional): `string` - The name of the sheet to search in.
|
139
|
+
- **or** (optional): `boolean` - If true, uses OR logic for the search.
|
140
|
+
- **query**: `string` - The search query.
|
141
|
+
|
142
|
+
#### Example
|
143
|
+
|
144
|
+
```typescript
|
145
|
+
sheet2db.Search({ query: 'name=John', sheet: 'Sheet1' })
|
146
|
+
.then(records => console.log(records))
|
147
|
+
.catch(error => console.error(error));
|
148
|
+
```
|
149
|
+
|
150
|
+
### Insert
|
151
|
+
|
152
|
+
Inserts data into the spreadsheet.
|
153
|
+
|
154
|
+
#### Parameters
|
155
|
+
|
156
|
+
- **data**: `Record<string, any> | Record<string, any>[]` - The data to insert.
|
157
|
+
- **sheet** (optional): `string` - The name of the sheet to insert data into.
|
158
|
+
|
159
|
+
#### Example
|
160
|
+
|
161
|
+
```typescript
|
162
|
+
sheet2db.Insert({ data: { name: 'John', age: 30 }, sheet: 'Sheet1' })
|
163
|
+
.then(response => console.log(response))
|
164
|
+
.catch(error => console.error(error));
|
165
|
+
```
|
166
|
+
|
167
|
+
### UpdateRow
|
168
|
+
|
169
|
+
Updates a specific row in the spreadsheet.
|
170
|
+
|
171
|
+
#### Parameters
|
172
|
+
|
173
|
+
- **row**: `number` - The row number to update.
|
174
|
+
- **data**: `Record<string, any>` - The data to update.
|
175
|
+
- **sheet** (optional): `string` - The name of the sheet to update data in.
|
176
|
+
|
177
|
+
#### Example
|
178
|
+
|
179
|
+
```typescript
|
180
|
+
sheet2db.UpdateRow({ row: 1, data: { age: 31 }, sheet: 'Sheet1' })
|
181
|
+
.then(response => console.log(response))
|
182
|
+
.catch(error => console.error(error));
|
183
|
+
```
|
184
|
+
|
185
|
+
### UpdateWithQuery
|
186
|
+
|
187
|
+
Updates rows based on a query.
|
188
|
+
|
189
|
+
#### Parameters
|
190
|
+
|
191
|
+
- **sheet** (optional): `string` - The name of the sheet to update data in.
|
192
|
+
- **query**: `string` - The query to identify rows to update.
|
193
|
+
- **data**: `Record<string, any>` - The data to update.
|
194
|
+
|
195
|
+
#### Example
|
196
|
+
|
197
|
+
```typescript
|
198
|
+
sheet2db.UpdateWithQuery({ query: 'name=John', data: { age: 32 }, sheet: 'Sheet1' })
|
199
|
+
.then(response => console.log(response))
|
200
|
+
.catch(error => console.error(error));
|
201
|
+
```
|
202
|
+
|
203
|
+
### BatchUpdate
|
204
|
+
|
205
|
+
Updates multiple records in batches.
|
206
|
+
|
207
|
+
#### Parameters
|
208
|
+
|
209
|
+
- **sheet** (optional): `string` - The name of the sheet to update data in.
|
210
|
+
- **batches**: `{ query: string, record: Record<string, any> }[]` - The batch update data.
|
211
|
+
|
212
|
+
#### Example
|
213
|
+
|
214
|
+
```typescript
|
215
|
+
sheet2db.BatchUpdate({ batches: [{ query: 'name=John', record: { age: 33 } }], sheet: 'Sheet1' })
|
216
|
+
.then(response => console.log(response))
|
217
|
+
.catch(error => console.error(error));
|
218
|
+
```
|
219
|
+
|
220
|
+
### DeleteRow
|
221
|
+
|
222
|
+
Deletes a specific row in the spreadsheet.
|
223
|
+
|
224
|
+
#### Parameters
|
225
|
+
|
226
|
+
- **row**: `number` - The row number to delete.
|
227
|
+
- **sheet** (optional): `string` - The name of the sheet to delete the row from.
|
228
|
+
|
229
|
+
#### Example
|
230
|
+
|
231
|
+
```typescript
|
232
|
+
sheet2db.DeleteRow({ row: 1, sheet: 'Sheet1' })
|
233
|
+
.then(response => console.log(response))
|
234
|
+
.catch(error => console.error(error));
|
235
|
+
```
|
236
|
+
|
237
|
+
### DeleteWithQuery
|
238
|
+
|
239
|
+
Deletes rows based on a query.
|
240
|
+
|
241
|
+
#### Parameters
|
242
|
+
|
243
|
+
- **query**: `string` - The query to identify rows to delete.
|
244
|
+
- **sheet** (optional): `string` - The name of the sheet to delete rows from.
|
245
|
+
|
246
|
+
#### Example
|
247
|
+
|
248
|
+
```typescript
|
249
|
+
sheet2db.DeleteWithQuery({ query: 'name=John', sheet: 'Sheet1' })
|
250
|
+
.then(response => console.log(response))
|
251
|
+
.catch(error => console.error(error));
|
252
|
+
```
|
253
|
+
|
254
|
+
### Clear
|
255
|
+
|
256
|
+
Clears all data from a sheet.
|
257
|
+
|
258
|
+
#### Parameters
|
259
|
+
|
260
|
+
- **sheet**: `string` - The name of the sheet to clear.
|
261
|
+
|
262
|
+
#### Example
|
263
|
+
|
264
|
+
```typescript
|
265
|
+
sheet2db.Clear({ sheet: 'Sheet1' })
|
266
|
+
.then(response => console.log(response))
|
267
|
+
.catch(error => console.error(error));
|
268
|
+
```
|
269
|
+
|
270
|
+
### CreateSheet
|
271
|
+
|
272
|
+
Creates a new sheet in the spreadsheet.
|
273
|
+
|
274
|
+
#### Parameters
|
275
|
+
|
276
|
+
- **title** (optional): `string` - The title of the new sheet.
|
277
|
+
|
278
|
+
#### Example
|
279
|
+
|
280
|
+
```typescript
|
281
|
+
sheet2db.CreateSheet({ title: 'NewSheet' })
|
282
|
+
.then(response => console.log(response))
|
283
|
+
.catch(error => console.error(error));
|
284
|
+
```
|
285
|
+
|
286
|
+
### DeleteSheet
|
287
|
+
|
288
|
+
Deletes a sheet from the spreadsheet.
|
289
|
+
|
290
|
+
#### Parameters
|
291
|
+
|
292
|
+
- **sheet**: `string` - The name of the sheet to delete.
|
293
|
+
|
294
|
+
#### Example
|
295
|
+
|
296
|
+
```typescript
|
297
|
+
sheet2db.DeleteSheet({ sheet: 'Sheet1' })
|
298
|
+
.then(response => console.log(response))
|
299
|
+
.catch(error => console.error(error));
|
300
|
+
```
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
## Environment Detection
|
305
|
+
|
306
|
+
The library automatically detects the environment (browser or Node.js) and uses the appropriate `fetch` implementation.
|
307
|
+
|
308
|
+
```typescript
|
309
|
+
let fetchFunction: typeof fetch;
|
310
|
+
|
311
|
+
if (typeof window !== 'undefined' && typeof window.fetch !== 'undefined') {
|
312
|
+
fetchFunction = window.fetch.bind(window);
|
313
|
+
} else {
|
314
|
+
fetchFunction = async (...args) => {
|
315
|
+
const fetch = global.fetch;
|
316
|
+
return fetch(...args);
|
317
|
+
};
|
318
|
+
}
|
319
|
+
```
|
320
|
+
|
321
|
+
## Error Handling
|
322
|
+
|
323
|
+
Each method returns a promise that resolves to the API response or rejects with an error.
|
324
|
+
|
325
|
+
```typescript
|
326
|
+
sheet2db.ReadContent()
|
327
|
+
.then(data => console.log(data))
|
328
|
+
.catch(error => console.error(error));
|
329
|
+
```
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@sheet2db/sdk",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.2",
|
4
4
|
"description": "",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"scripts": {
|
@@ -14,7 +14,7 @@
|
|
14
14
|
}
|
15
15
|
},
|
16
16
|
"author": "",
|
17
|
-
"license": "
|
17
|
+
"license": "MIT",
|
18
18
|
"devDependencies": {
|
19
19
|
"@jest/globals": "^29.7.0",
|
20
20
|
"jest": "^29.7.0",
|