@moonhr/sheets-client 0.1.0 → 0.1.1
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 +204 -12
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -1,15 +1,207 @@
|
|
|
1
1
|
# @moonhr/sheets-client
|
|
2
2
|
|
|
3
|
+
[English](#english) | [한국어](#한국어)
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## English
|
|
8
|
+
|
|
9
|
+
A lightweight Google Sheets client that eliminates the authentication and CRUD boilerplate when using Sheets as a form data store.
|
|
10
|
+
Includes service account auth, row append/query/update, and phone number cell formatting.
|
|
11
|
+
|
|
12
|
+
### Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @moonhr/sheets-client
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Prerequisites
|
|
19
|
+
|
|
20
|
+
1. Create a service account in [Google Cloud Console](https://console.cloud.google.com/) and download the JSON key.
|
|
21
|
+
2. Share the target spreadsheet with the service account email as an **Editor**.
|
|
22
|
+
3. Set the following environment variables.
|
|
23
|
+
|
|
24
|
+
```env
|
|
25
|
+
GOOGLE_SERVICE_ACCOUNT_KEY={"client_email":"...","private_key":"..."}
|
|
26
|
+
GOOGLE_SHEETS_SPREADSHEET_ID=1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms
|
|
27
|
+
GOOGLE_SHEETS_SHEET_NAME=Sheet1
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Basic Usage
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { createSheetsClient } from '@moonhr/sheets-client'
|
|
34
|
+
|
|
35
|
+
const client = createSheetsClient({
|
|
36
|
+
serviceAccountKey: process.env.GOOGLE_SERVICE_ACCOUNT_KEY,
|
|
37
|
+
spreadsheetId: process.env.GOOGLE_SHEETS_SPREADSHEET_ID,
|
|
38
|
+
sheetName: process.env.GOOGLE_SHEETS_SHEET_NAME,
|
|
39
|
+
})
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### API
|
|
43
|
+
|
|
44
|
+
#### `appendRow(values, range?)`
|
|
45
|
+
|
|
46
|
+
Appends data to the last row of the sheet.
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
await client.appendRow(['2024-01-01', 'John Doe', 'Acme Corp', '01012345678'])
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
| Parameter | Type | Default | Description |
|
|
53
|
+
|---|---|---|---|
|
|
54
|
+
| `values` | `string[]` | — | Array of cell values |
|
|
55
|
+
| `range` | `string` | `'A:Z'` | Target range |
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
#### `getRows(range?)`
|
|
60
|
+
|
|
61
|
+
Returns all rows in the specified range. Each cell is a trimmed string.
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
const rows = await client.getRows('A:E')
|
|
65
|
+
// [['Header1', 'Header2', ...], ['Value1', 'Value2', ...], ...]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
#### `findRow(matcher, options?)`
|
|
71
|
+
|
|
72
|
+
Returns the first row matching the condition, or `null` if not found.
|
|
73
|
+
|
|
74
|
+
**Match by column index (0-based)**
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const result = await client.findRow({ 1: 'John Doe', 2: 'Acme Corp' })
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Performs a trim + lowercase comparison on both sides.
|
|
81
|
+
|
|
82
|
+
**Match with a custom function**
|
|
83
|
+
|
|
84
|
+
Use this when you need numeric comparison, e.g. for phone numbers.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { digitsOnly } from '@moonhr/sheets-client'
|
|
88
|
+
|
|
89
|
+
const result = await client.findRow((row) => {
|
|
90
|
+
return row[1].trim().toLowerCase() === 'john doe' &&
|
|
91
|
+
digitsOnly(row[3]) === digitsOnly('010-1234-5678')
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**Return value**
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
{
|
|
99
|
+
rowIndex: number // 1-based sheet row number — pass directly to updateRow
|
|
100
|
+
values: string[] // Array of cell values in that row
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**options**
|
|
105
|
+
|
|
106
|
+
| Option | Type | Default | Description |
|
|
107
|
+
|---|---|---|---|
|
|
108
|
+
| `range` | `string` | `'A:Z'` | Search range |
|
|
109
|
+
| `skipHeader` | `boolean` | `true` | Whether to skip the first (header) row |
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
#### `updateRow(rowIndex, values, startCol?)`
|
|
114
|
+
|
|
115
|
+
Updates cells in a specific row.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
// Use the rowIndex returned by findRow directly
|
|
119
|
+
await client.updateRow(result.rowIndex, ['John Doe', 'New Corp', '01099998888'], 'B')
|
|
120
|
+
// → Updates range B{rowIndex}:D{rowIndex}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
| Parameter | Type | Default | Description |
|
|
124
|
+
|---|---|---|---|
|
|
125
|
+
| `rowIndex` | `number` | — | Sheet row number (2 or greater) |
|
|
126
|
+
| `values` | `string[]` | — | Array of replacement cell values |
|
|
127
|
+
| `startCol` | `string` | `'A'` | Starting column letter |
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
### Utilities
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { toTextCell, digitsOnly, stripQuotes } from '@moonhr/sheets-client'
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
| Function | Description | Example |
|
|
138
|
+
|---|---|---|
|
|
139
|
+
| `toTextCell(value)` | Forces a phone number to be saved as text in Sheets (prevents auto-conversion to number) | `'01012345678'` → `"'01012345678"` |
|
|
140
|
+
| `digitsOnly(value)` | Extracts digits only (for phone number comparison) | `'010-1234-5678'` → `'01012345678'` |
|
|
141
|
+
| `stripQuotes(value)` | Removes wrapping quotes from environment variable values | `'"Sheet1"'` → `'Sheet1'` |
|
|
142
|
+
|
|
143
|
+
### Real-world Example — RSVP Form
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
import { createSheetsClient, toTextCell, digitsOnly } from '@moonhr/sheets-client'
|
|
147
|
+
|
|
148
|
+
const client = createSheetsClient({
|
|
149
|
+
serviceAccountKey: process.env.GOOGLE_SERVICE_ACCOUNT_KEY,
|
|
150
|
+
spreadsheetId: process.env.GOOGLE_SHEETS_SPREADSHEET_ID,
|
|
151
|
+
sheetName: process.env.GOOGLE_SHEETS_SHEET_NAME,
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
// Submit registration
|
|
155
|
+
await client.appendRow([
|
|
156
|
+
new Date().toISOString(),
|
|
157
|
+
'John Doe',
|
|
158
|
+
'Acme Corp',
|
|
159
|
+
toTextCell('01012345678'),
|
|
160
|
+
'john@example.com',
|
|
161
|
+
'Agreed',
|
|
162
|
+
])
|
|
163
|
+
|
|
164
|
+
// Look up registration
|
|
165
|
+
const found = await client.findRow((row) =>
|
|
166
|
+
row[1].trim() === 'John Doe' &&
|
|
167
|
+
digitsOnly(row[3]) === '01012345678'
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
// Update registration
|
|
171
|
+
if (found) {
|
|
172
|
+
await client.updateRow(
|
|
173
|
+
found.rowIndex,
|
|
174
|
+
['John Doe', 'New Corp', toTextCell('01099998888'), 'new@example.com'],
|
|
175
|
+
'B'
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Build
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
npm run build # generate dist/
|
|
184
|
+
npm run dev # watch mode
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### License
|
|
188
|
+
|
|
189
|
+
MIT
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## 한국어
|
|
194
|
+
|
|
3
195
|
Google Sheets를 폼 데이터 저장소로 사용할 때 필요한 인증·CRUD 보일러플레이트를 제거해주는 경량 클라이언트입니다.
|
|
4
196
|
서비스 계정 인증, 행 추가/조회/수정, 전화번호 셀 포맷 처리를 포함합니다.
|
|
5
197
|
|
|
6
|
-
|
|
198
|
+
### 설치
|
|
7
199
|
|
|
8
200
|
```bash
|
|
9
201
|
npm install @moonhr/sheets-client
|
|
10
202
|
```
|
|
11
203
|
|
|
12
|
-
|
|
204
|
+
### 사전 준비
|
|
13
205
|
|
|
14
206
|
1. [Google Cloud Console](https://console.cloud.google.com/)에서 서비스 계정을 생성하고 JSON 키를 발급합니다.
|
|
15
207
|
2. 해당 서비스 계정 이메일을 대상 스프레드시트에 **편집자** 권한으로 공유합니다.
|
|
@@ -21,7 +213,7 @@ GOOGLE_SHEETS_SPREADSHEET_ID=1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms
|
|
|
21
213
|
GOOGLE_SHEETS_SHEET_NAME=Sheet1
|
|
22
214
|
```
|
|
23
215
|
|
|
24
|
-
|
|
216
|
+
### 기본 사용법
|
|
25
217
|
|
|
26
218
|
```typescript
|
|
27
219
|
import { createSheetsClient } from '@moonhr/sheets-client'
|
|
@@ -33,9 +225,9 @@ const client = createSheetsClient({
|
|
|
33
225
|
})
|
|
34
226
|
```
|
|
35
227
|
|
|
36
|
-
|
|
228
|
+
### API
|
|
37
229
|
|
|
38
|
-
|
|
230
|
+
#### `appendRow(values, range?)`
|
|
39
231
|
|
|
40
232
|
시트 마지막 행에 데이터를 추가합니다.
|
|
41
233
|
|
|
@@ -50,7 +242,7 @@ await client.appendRow(['2024-01-01', '홍길동', '회사명', '01012345678'])
|
|
|
50
242
|
|
|
51
243
|
---
|
|
52
244
|
|
|
53
|
-
|
|
245
|
+
#### `getRows(range?)`
|
|
54
246
|
|
|
55
247
|
지정 범위의 모든 행을 반환합니다. 각 셀은 trim된 문자열입니다.
|
|
56
248
|
|
|
@@ -61,7 +253,7 @@ const rows = await client.getRows('A:E')
|
|
|
61
253
|
|
|
62
254
|
---
|
|
63
255
|
|
|
64
|
-
|
|
256
|
+
#### `findRow(matcher, options?)`
|
|
65
257
|
|
|
66
258
|
조건에 맞는 첫 번째 행을 반환합니다. 없으면 `null`.
|
|
67
259
|
|
|
@@ -104,7 +296,7 @@ const result = await client.findRow((row) => {
|
|
|
104
296
|
|
|
105
297
|
---
|
|
106
298
|
|
|
107
|
-
|
|
299
|
+
#### `updateRow(rowIndex, values, startCol?)`
|
|
108
300
|
|
|
109
301
|
특정 행의 셀을 업데이트합니다.
|
|
110
302
|
|
|
@@ -122,7 +314,7 @@ await client.updateRow(result.rowIndex, ['홍길동', '새회사', '01099998888'
|
|
|
122
314
|
|
|
123
315
|
---
|
|
124
316
|
|
|
125
|
-
|
|
317
|
+
### 유틸리티
|
|
126
318
|
|
|
127
319
|
```typescript
|
|
128
320
|
import { toTextCell, digitsOnly, stripQuotes } from '@moonhr/sheets-client'
|
|
@@ -134,7 +326,7 @@ import { toTextCell, digitsOnly, stripQuotes } from '@moonhr/sheets-client'
|
|
|
134
326
|
| `digitsOnly(value)` | 숫자만 추출 (전화번호 비교용) | `'010-1234-5678'` → `'01012345678'` |
|
|
135
327
|
| `stripQuotes(value)` | 환경변수 래핑 따옴표 제거 | `'"Sheet1"'` → `'Sheet1'` |
|
|
136
328
|
|
|
137
|
-
|
|
329
|
+
### 실사용 예시 — RSVP 폼
|
|
138
330
|
|
|
139
331
|
```typescript
|
|
140
332
|
import { createSheetsClient, toTextCell, digitsOnly } from '@moonhr/sheets-client'
|
|
@@ -171,13 +363,13 @@ if (found) {
|
|
|
171
363
|
}
|
|
172
364
|
```
|
|
173
365
|
|
|
174
|
-
|
|
366
|
+
### 빌드
|
|
175
367
|
|
|
176
368
|
```bash
|
|
177
369
|
npm run build # dist/ 생성
|
|
178
370
|
npm run dev # watch 모드
|
|
179
371
|
```
|
|
180
372
|
|
|
181
|
-
|
|
373
|
+
### 라이선스
|
|
182
374
|
|
|
183
375
|
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moonhr/sheets-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Google Sheets client for form-based data collection",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
"require": "./dist/index.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
"files": [
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
16
18
|
"scripts": {
|
|
17
19
|
"build": "tsup",
|
|
18
20
|
"dev": "tsup --watch"
|
|
@@ -24,6 +26,10 @@
|
|
|
24
26
|
"tsup": "^8.0.0",
|
|
25
27
|
"typescript": "^5.0.0"
|
|
26
28
|
},
|
|
27
|
-
"keywords": [
|
|
29
|
+
"keywords": [
|
|
30
|
+
"google-sheets",
|
|
31
|
+
"spreadsheet",
|
|
32
|
+
"form"
|
|
33
|
+
],
|
|
28
34
|
"license": "MIT"
|
|
29
35
|
}
|