google-sheets-mapper 1.0.1 → 2.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 +27 -20
- package/dist/index.cjs +103 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +22 -6
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +98 -6
- package/package.json +39 -44
- package/src/index.ts +8 -13
- package/src/types.ts +9 -4
- package/src/utils.ts +59 -51
- package/dist/google-sheets-mapper.cjs.development.js +0 -1150
- package/dist/google-sheets-mapper.cjs.development.js.map +0 -1
- package/dist/google-sheets-mapper.cjs.production.min.js +0 -2
- package/dist/google-sheets-mapper.cjs.production.min.js.map +0 -1
- package/dist/google-sheets-mapper.esm.js +0 -1146
- package/dist/google-sheets-mapper.esm.js.map +0 -1
- package/dist/types.d.ts +0 -35
- package/dist/utils.d.ts +0 -4
package/README.md
CHANGED
|
@@ -8,16 +8,16 @@
|
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
11
|
-
Package can be added using **yarn**:
|
|
12
|
-
|
|
13
11
|
```bash
|
|
14
|
-
|
|
12
|
+
npm install google-sheets-mapper
|
|
15
13
|
```
|
|
16
14
|
|
|
17
|
-
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add google-sheets-mapper
|
|
17
|
+
```
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
|
-
|
|
20
|
+
bun add google-sheets-mapper
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
UMD build available on [unpkg](https://www.unpkg.com/browse/google-sheets-mapper@1.0.0/dist/google-sheets-mapper.cjs.production.min.js).
|
|
@@ -44,7 +44,7 @@ https://docs.google.com/spreadsheets/d/[THIS-IS-THE-SHEET-ID]/
|
|
|
44
44
|
### Get data from all sheets inside the spreadsheet
|
|
45
45
|
|
|
46
46
|
```js
|
|
47
|
-
import { fetchGoogleSheetsData } from
|
|
47
|
+
import { fetchGoogleSheetsData } from "google-sheets-mapper";
|
|
48
48
|
|
|
49
49
|
const getData = async () => {
|
|
50
50
|
try {
|
|
@@ -63,14 +63,14 @@ const getData = async () => {
|
|
|
63
63
|
Don't use single quotes on sheet names, they will be removed because when using space in sheet name it will be returned wrapped with single quotes and plugin will remove them for clean string id.
|
|
64
64
|
|
|
65
65
|
```js
|
|
66
|
-
import { fetchGoogleSheetsData } from
|
|
66
|
+
import { fetchGoogleSheetsData } from "google-sheets-mapper";
|
|
67
67
|
|
|
68
68
|
const getData = async () => {
|
|
69
69
|
try {
|
|
70
70
|
return await fetchGoogleSheetsData({
|
|
71
71
|
apiKey: process.env.REACT_APP_GOOGLE_API_KEY,
|
|
72
72
|
sheetId: process.env.REACT_APP_GOOGLE_SHEETS_ID,
|
|
73
|
-
|
|
73
|
+
sheetsOptions: [{ id: "Sheet1" }],
|
|
74
74
|
});
|
|
75
75
|
} catch (error) {
|
|
76
76
|
console.error(error);
|
|
@@ -84,15 +84,15 @@ const getData = async () => {
|
|
|
84
84
|
|
|
85
85
|
The `GoogleSheetsMapper.fetchGoogleSheetsData` function takes an object with three properties:
|
|
86
86
|
|
|
87
|
-
| Name
|
|
88
|
-
|
|
|
89
|
-
| apiKey
|
|
90
|
-
| sheetId
|
|
91
|
-
|
|
|
87
|
+
| Name | Value |
|
|
88
|
+
| ------------- | ------ |
|
|
89
|
+
| apiKey | string |
|
|
90
|
+
| sheetId | string |
|
|
91
|
+
| sheetsOptions | array |
|
|
92
92
|
|
|
93
93
|
- `apiKey` is a Google Sheets API v4 key from [Google Cloud Console](https://console.cloud.google.com/).
|
|
94
94
|
- `sheetId` is the id of the sheet.
|
|
95
|
-
- `
|
|
95
|
+
- `sheetsOptions` is an array of specific objects `{ id, headerRowIndex }`. Can be left out then it will fallback to all sheets inside the spreadsheet and use first row from sheet as header.
|
|
96
96
|
|
|
97
97
|
### Exposed Data
|
|
98
98
|
|
|
@@ -109,19 +109,20 @@ try {
|
|
|
109
109
|
}
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
-
| Name
|
|
113
|
-
|
|
|
114
|
-
| data
|
|
112
|
+
| Name | Value |
|
|
113
|
+
| ----- | -------------- |
|
|
114
|
+
| data | array |
|
|
115
|
+
| error | null or object |
|
|
115
116
|
|
|
116
117
|
- `data` is an array of mapped data objects.
|
|
117
118
|
|
|
118
119
|
```js
|
|
119
120
|
[
|
|
120
121
|
{
|
|
121
|
-
id:
|
|
122
|
+
id: "Sheet1",
|
|
122
123
|
data: [
|
|
123
|
-
{ value:
|
|
124
|
-
{ value:
|
|
124
|
+
{ value: "et", key: "language" },
|
|
125
|
+
{ value: "Test sheet", key: "title" },
|
|
125
126
|
],
|
|
126
127
|
},
|
|
127
128
|
];
|
|
@@ -136,3 +137,9 @@ try {
|
|
|
136
137
|
url: 'https://sheets.googleapis.com/v4/spreadsheets/...',
|
|
137
138
|
}
|
|
138
139
|
```
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## Migration from v1 to v2
|
|
144
|
+
|
|
145
|
+
- Change `sheetsNames` array of string to `sheetsOptions` array of objects with `{ id: 'sheetName' }`
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
const GOOGLE_API_URL = "https://sheets.googleapis.com/v4/spreadsheets";
|
|
4
|
+
const getRanges = (sheetNames = [])=>{
|
|
5
|
+
// ranges=Sheet1&ranges=Sheet2
|
|
6
|
+
return sheetNames.map((sheetName)=>`ranges=${sheetName}`).join("&");
|
|
7
|
+
};
|
|
8
|
+
const getSheetsTitleUrl = (sheetId, apiKey)=>{
|
|
9
|
+
return `${GOOGLE_API_URL}/${sheetId}?fields=sheets%2Fproperties%2Ftitle&key=${apiKey}`;
|
|
10
|
+
};
|
|
11
|
+
const getBatchUrl = (sheetId, ranges, apiKey)=>{
|
|
12
|
+
const rangesQueryString = getRanges(ranges);
|
|
13
|
+
return `${GOOGLE_API_URL}/${sheetId}?${rangesQueryString}&key=${apiKey}&includeGridData=true`;
|
|
14
|
+
};
|
|
15
|
+
class ApiResponseError extends Error {
|
|
16
|
+
constructor(message, response){
|
|
17
|
+
super(message);
|
|
18
|
+
Object.setPrototypeOf(this, ApiResponseError.prototype);
|
|
19
|
+
this.response = response;
|
|
20
|
+
if (Error.captureStackTrace) {
|
|
21
|
+
Error.captureStackTrace(this, ApiResponseError);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const makeFetch = async (url, config = {})=>{
|
|
26
|
+
const response = await fetch(url, config);
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
throw new ApiResponseError(`Request to '${url}' failed with ${response.status}${response.statusText ? `: ${response.statusText}` : ""}`, {
|
|
29
|
+
status: response.status,
|
|
30
|
+
statusText: response.statusText,
|
|
31
|
+
url: response.url
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return await response.json();
|
|
35
|
+
};
|
|
36
|
+
const mapRecords = (records, headerData)=>{
|
|
37
|
+
return records.filter((record)=>record.length > 0).map((record)=>record.reduce((obj, item, index)=>{
|
|
38
|
+
const key = headerData[index];
|
|
39
|
+
if (key !== undefined) {
|
|
40
|
+
obj[key] = item;
|
|
41
|
+
}
|
|
42
|
+
return obj;
|
|
43
|
+
}, {}));
|
|
44
|
+
};
|
|
45
|
+
const mapData = ({ sheets, sheetsOptions = [] })=>{
|
|
46
|
+
return sheets.map((sheet)=>{
|
|
47
|
+
const id = sheet.range.split("!")[0].replace(/'/g, "");
|
|
48
|
+
const rows = sheet.values || [];
|
|
49
|
+
if (rows.length > 0) {
|
|
50
|
+
const sheetsOptionsSheet = sheetsOptions.find((option)=>option.id === id);
|
|
51
|
+
const headerRowIndex = sheetsOptionsSheet?.headerRowIndex ?? 0;
|
|
52
|
+
const header = rows[headerRowIndex];
|
|
53
|
+
const records = rows.filter((_, index)=>index > headerRowIndex);
|
|
54
|
+
const recordsData = mapRecords(records, header);
|
|
55
|
+
return {
|
|
56
|
+
id,
|
|
57
|
+
data: recordsData
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
id,
|
|
62
|
+
data: []
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
const fetchBatchData = async ({ apiKey, sheetId, sheetsOptions = [] })=>{
|
|
67
|
+
const sheetsNames = sheetsOptions.map((option)=>option.id);
|
|
68
|
+
const url = getBatchUrl(sheetId, sheetsNames, apiKey);
|
|
69
|
+
return await makeFetch(url);
|
|
70
|
+
};
|
|
71
|
+
const fetchAllSheetsData = async ({ apiKey, sheetId })=>{
|
|
72
|
+
const urlTitles = getSheetsTitleUrl(sheetId, apiKey);
|
|
73
|
+
const { sheets } = await makeFetch(urlTitles);
|
|
74
|
+
const sheetsOptions = sheets.map((sheet)=>({
|
|
75
|
+
id: sheet.properties.title
|
|
76
|
+
}));
|
|
77
|
+
return await fetchBatchData({
|
|
78
|
+
apiKey,
|
|
79
|
+
sheetId,
|
|
80
|
+
sheetsOptions
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const GoogleSheetsMapper = {
|
|
85
|
+
async fetchGoogleSheetsData ({ apiKey, sheetId, sheetsOptions = [] }) {
|
|
86
|
+
const response = sheetsOptions.length === 0 ? await fetchAllSheetsData({
|
|
87
|
+
apiKey,
|
|
88
|
+
sheetId
|
|
89
|
+
}) : await fetchBatchData({
|
|
90
|
+
apiKey,
|
|
91
|
+
sheetId,
|
|
92
|
+
sheetsOptions
|
|
93
|
+
});
|
|
94
|
+
return mapData({
|
|
95
|
+
sheets: response.valueRanges,
|
|
96
|
+
sheetsOptions
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const fetchGoogleSheetsData = GoogleSheetsMapper.fetchGoogleSheetsData;
|
|
101
|
+
|
|
102
|
+
exports.default = GoogleSheetsMapper;
|
|
103
|
+
exports.fetchGoogleSheetsData = fetchGoogleSheetsData;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface SheetsOption {
|
|
2
|
+
id: string;
|
|
3
|
+
headerRowIndex?: number;
|
|
4
|
+
}
|
|
5
|
+
interface MapperOptions {
|
|
6
|
+
apiKey: string;
|
|
7
|
+
sheetId: string;
|
|
8
|
+
sheetsOptions?: SheetsOption[];
|
|
9
|
+
}
|
|
10
|
+
interface MapperState {
|
|
11
|
+
id: string;
|
|
12
|
+
data: Record<string, string>[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare const GoogleSheetsMapper: {
|
|
16
|
+
fetchGoogleSheetsData({ apiKey, sheetId, sheetsOptions, }: MapperOptions): Promise<MapperState[]>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
declare const fetchGoogleSheetsData: ({ apiKey, sheetId, sheetsOptions, }: MapperOptions) => Promise<MapperState[]>;
|
|
20
|
+
|
|
21
|
+
export { GoogleSheetsMapper as default, fetchGoogleSheetsData };
|
|
22
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/index.ts"],"sourcesContent":["export interface SheetsOption {\n id: string;\n headerRowIndex?: number;\n}\n\nexport interface MapperOptions {\n apiKey: string;\n sheetId: string;\n sheetsOptions?: SheetsOption[];\n}\n\nexport interface ValueRange {\n majorDimensions: string;\n range: string;\n values: string[][];\n}\n\nexport interface ValueRangesResponse {\n spreadsheetId: string;\n valueRanges: ValueRange[];\n}\n\nexport interface PropertiesFromResponse {\n title: string;\n}\n\nexport interface SheetFromResponse {\n properties: PropertiesFromResponse;\n}\n\nexport interface SheetsResponse {\n sheets: SheetFromResponse[];\n}\n\nexport interface MapperState {\n id: string;\n data: Record<string, string>[];\n}\n\nexport interface ApiResponse {\n url: string;\n status: number;\n statusText: string;\n}\n\nexport interface ErrorResponse {\n response: ApiResponse;\n}\n","import type { MapperOptions, MapperState, ValueRangesResponse } from \"./types\";\nimport { fetchBatchData, fetchAllSheetsData, mapData } from \"./utils\";\n\nconst GoogleSheetsMapper = {\n async fetchGoogleSheetsData({\n apiKey,\n sheetId,\n sheetsOptions = [],\n }: MapperOptions): Promise<MapperState[]> {\n const response: ValueRangesResponse =\n sheetsOptions.length === 0\n ? await fetchAllSheetsData({ apiKey, sheetId })\n : await fetchBatchData({ apiKey, sheetId, sheetsOptions });\n return mapData({ sheets: response.valueRanges, sheetsOptions });\n },\n};\n\nexport default GoogleSheetsMapper;\nexport const fetchGoogleSheetsData = GoogleSheetsMapper.fetchGoogleSheetsData;\n"],"names":[],"mappings":"AAAO,UAAA,YAAA;AACP;AACA;AACA;AACO,UAAA,aAAA;AACP;AACA;AACA,oBAAA,YAAA;AACA;AAmBO,UAAA,WAAA;AACP;AACA,UAAA,MAAA;AACA;;AC7BA,cAAA,kBAAA;AACA,+DAAA,aAAA,GAAA,OAAA,CAAA,WAAA;AACA;;AAEO,cAAA,qBAAA,wCAAA,aAAA,KAAA,OAAA,CAAA,WAAA;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
interface SheetsOption {
|
|
2
|
+
id: string;
|
|
3
|
+
headerRowIndex?: number;
|
|
4
|
+
}
|
|
5
|
+
interface MapperOptions {
|
|
6
|
+
apiKey: string;
|
|
7
|
+
sheetId: string;
|
|
8
|
+
sheetsOptions?: SheetsOption[];
|
|
9
|
+
}
|
|
10
|
+
interface MapperState {
|
|
11
|
+
id: string;
|
|
12
|
+
data: Record<string, string>[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare const GoogleSheetsMapper: {
|
|
16
|
+
fetchGoogleSheetsData({ apiKey, sheetId, sheetsOptions, }: MapperOptions): Promise<MapperState[]>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
declare const fetchGoogleSheetsData: ({ apiKey, sheetId, sheetsOptions, }: MapperOptions) => Promise<MapperState[]>;
|
|
20
|
+
|
|
21
|
+
export { GoogleSheetsMapper as default, fetchGoogleSheetsData };
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../src/types.ts","../src/index.ts"],"sourcesContent":["export interface SheetsOption {\n id: string;\n headerRowIndex?: number;\n}\n\nexport interface MapperOptions {\n apiKey: string;\n sheetId: string;\n sheetsOptions?: SheetsOption[];\n}\n\nexport interface ValueRange {\n majorDimensions: string;\n range: string;\n values: string[][];\n}\n\nexport interface ValueRangesResponse {\n spreadsheetId: string;\n valueRanges: ValueRange[];\n}\n\nexport interface PropertiesFromResponse {\n title: string;\n}\n\nexport interface SheetFromResponse {\n properties: PropertiesFromResponse;\n}\n\nexport interface SheetsResponse {\n sheets: SheetFromResponse[];\n}\n\nexport interface MapperState {\n id: string;\n data: Record<string, string>[];\n}\n\nexport interface ApiResponse {\n url: string;\n status: number;\n statusText: string;\n}\n\nexport interface ErrorResponse {\n response: ApiResponse;\n}\n","import type { MapperOptions, MapperState, ValueRangesResponse } from \"./types\";\nimport { fetchBatchData, fetchAllSheetsData, mapData } from \"./utils\";\n\nconst GoogleSheetsMapper = {\n async fetchGoogleSheetsData({\n apiKey,\n sheetId,\n sheetsOptions = [],\n }: MapperOptions): Promise<MapperState[]> {\n const response: ValueRangesResponse =\n sheetsOptions.length === 0\n ? await fetchAllSheetsData({ apiKey, sheetId })\n : await fetchBatchData({ apiKey, sheetId, sheetsOptions });\n return mapData({ sheets: response.valueRanges, sheetsOptions });\n },\n};\n\nexport default GoogleSheetsMapper;\nexport const fetchGoogleSheetsData = GoogleSheetsMapper.fetchGoogleSheetsData;\n"],"names":[],"mappings":"AAAO,UAAA,YAAA;AACP;AACA;AACA;AACO,UAAA,aAAA;AACP;AACA;AACA,oBAAA,YAAA;AACA;AAmBO,UAAA,WAAA;AACP;AACA,UAAA,MAAA;AACA;;AC7BA,cAAA,kBAAA;AACA,+DAAA,aAAA,GAAA,OAAA,CAAA,WAAA;AACA;;AAEO,cAAA,qBAAA,wCAAA,aAAA,KAAA,OAAA,CAAA,WAAA;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,100 @@
|
|
|
1
|
+
const GOOGLE_API_URL = "https://sheets.googleapis.com/v4/spreadsheets";
|
|
2
|
+
const getRanges = (sheetNames = [])=>{
|
|
3
|
+
// ranges=Sheet1&ranges=Sheet2
|
|
4
|
+
return sheetNames.map((sheetName)=>`ranges=${sheetName}`).join("&");
|
|
5
|
+
};
|
|
6
|
+
const getSheetsTitleUrl = (sheetId, apiKey)=>{
|
|
7
|
+
return `${GOOGLE_API_URL}/${sheetId}?fields=sheets%2Fproperties%2Ftitle&key=${apiKey}`;
|
|
8
|
+
};
|
|
9
|
+
const getBatchUrl = (sheetId, ranges, apiKey)=>{
|
|
10
|
+
const rangesQueryString = getRanges(ranges);
|
|
11
|
+
return `${GOOGLE_API_URL}/${sheetId}?${rangesQueryString}&key=${apiKey}&includeGridData=true`;
|
|
12
|
+
};
|
|
13
|
+
class ApiResponseError extends Error {
|
|
14
|
+
constructor(message, response){
|
|
15
|
+
super(message);
|
|
16
|
+
Object.setPrototypeOf(this, ApiResponseError.prototype);
|
|
17
|
+
this.response = response;
|
|
18
|
+
if (Error.captureStackTrace) {
|
|
19
|
+
Error.captureStackTrace(this, ApiResponseError);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const makeFetch = async (url, config = {})=>{
|
|
24
|
+
const response = await fetch(url, config);
|
|
25
|
+
if (!response.ok) {
|
|
26
|
+
throw new ApiResponseError(`Request to '${url}' failed with ${response.status}${response.statusText ? `: ${response.statusText}` : ""}`, {
|
|
27
|
+
status: response.status,
|
|
28
|
+
statusText: response.statusText,
|
|
29
|
+
url: response.url
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return await response.json();
|
|
33
|
+
};
|
|
34
|
+
const mapRecords = (records, headerData)=>{
|
|
35
|
+
return records.filter((record)=>record.length > 0).map((record)=>record.reduce((obj, item, index)=>{
|
|
36
|
+
const key = headerData[index];
|
|
37
|
+
if (key !== undefined) {
|
|
38
|
+
obj[key] = item;
|
|
39
|
+
}
|
|
40
|
+
return obj;
|
|
41
|
+
}, {}));
|
|
42
|
+
};
|
|
43
|
+
const mapData = ({ sheets, sheetsOptions = [] })=>{
|
|
44
|
+
return sheets.map((sheet)=>{
|
|
45
|
+
const id = sheet.range.split("!")[0].replace(/'/g, "");
|
|
46
|
+
const rows = sheet.values || [];
|
|
47
|
+
if (rows.length > 0) {
|
|
48
|
+
const sheetsOptionsSheet = sheetsOptions.find((option)=>option.id === id);
|
|
49
|
+
const headerRowIndex = sheetsOptionsSheet?.headerRowIndex ?? 0;
|
|
50
|
+
const header = rows[headerRowIndex];
|
|
51
|
+
const records = rows.filter((_, index)=>index > headerRowIndex);
|
|
52
|
+
const recordsData = mapRecords(records, header);
|
|
53
|
+
return {
|
|
54
|
+
id,
|
|
55
|
+
data: recordsData
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
id,
|
|
60
|
+
data: []
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
const fetchBatchData = async ({ apiKey, sheetId, sheetsOptions = [] })=>{
|
|
65
|
+
const sheetsNames = sheetsOptions.map((option)=>option.id);
|
|
66
|
+
const url = getBatchUrl(sheetId, sheetsNames, apiKey);
|
|
67
|
+
return await makeFetch(url);
|
|
68
|
+
};
|
|
69
|
+
const fetchAllSheetsData = async ({ apiKey, sheetId })=>{
|
|
70
|
+
const urlTitles = getSheetsTitleUrl(sheetId, apiKey);
|
|
71
|
+
const { sheets } = await makeFetch(urlTitles);
|
|
72
|
+
const sheetsOptions = sheets.map((sheet)=>({
|
|
73
|
+
id: sheet.properties.title
|
|
74
|
+
}));
|
|
75
|
+
return await fetchBatchData({
|
|
76
|
+
apiKey,
|
|
77
|
+
sheetId,
|
|
78
|
+
sheetsOptions
|
|
79
|
+
});
|
|
80
|
+
};
|
|
1
81
|
|
|
2
|
-
|
|
82
|
+
const GoogleSheetsMapper = {
|
|
83
|
+
async fetchGoogleSheetsData ({ apiKey, sheetId, sheetsOptions = [] }) {
|
|
84
|
+
const response = sheetsOptions.length === 0 ? await fetchAllSheetsData({
|
|
85
|
+
apiKey,
|
|
86
|
+
sheetId
|
|
87
|
+
}) : await fetchBatchData({
|
|
88
|
+
apiKey,
|
|
89
|
+
sheetId,
|
|
90
|
+
sheetsOptions
|
|
91
|
+
});
|
|
92
|
+
return mapData({
|
|
93
|
+
sheets: response.valueRanges,
|
|
94
|
+
sheetsOptions
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const fetchGoogleSheetsData = GoogleSheetsMapper.fetchGoogleSheetsData;
|
|
3
99
|
|
|
4
|
-
|
|
5
|
-
module.exports = require('./google-sheets-mapper.cjs.production.min.js')
|
|
6
|
-
} else {
|
|
7
|
-
module.exports = require('./google-sheets-mapper.cjs.development.js')
|
|
8
|
-
}
|
|
100
|
+
export { GoogleSheetsMapper as default, fetchGoogleSheetsData };
|
package/package.json
CHANGED
|
@@ -1,67 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "google-sheets-mapper",
|
|
3
|
+
"version": "2.1.0",
|
|
3
4
|
"description": "### A library for getting data from Google Sheets API v4",
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
"keywords": [
|
|
6
|
+
"google-sheets",
|
|
7
|
+
"google-sheets-api",
|
|
8
|
+
"google-sheets-api-v4",
|
|
9
|
+
"google-sheets-mapper"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/gglukmann/use-google-sheets#readme",
|
|
7
12
|
"bugs": {
|
|
8
13
|
"url": "https://github.com/gglukmann/use-google-sheets/issues"
|
|
9
14
|
},
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"test": "test"
|
|
13
|
-
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Gert Glükmann <gglukmann@gmail.com>",
|
|
14
17
|
"repository": {
|
|
15
18
|
"type": "git",
|
|
16
19
|
"url": "git+https://github.com/gglukmann/google-sheets-mapper.git"
|
|
17
20
|
},
|
|
18
|
-
"keywords": [
|
|
19
|
-
"google-sheets-mapper",
|
|
20
|
-
"google-sheets",
|
|
21
|
-
"google-sheets-api",
|
|
22
|
-
"google-sheets-api-v4"
|
|
23
|
-
],
|
|
24
|
-
"main": "dist/index.js",
|
|
25
|
-
"typings": "dist/index.d.ts",
|
|
26
21
|
"files": [
|
|
27
22
|
"dist",
|
|
28
23
|
"src"
|
|
29
24
|
],
|
|
30
|
-
"
|
|
31
|
-
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.cjs",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/index.d.cts",
|
|
37
|
+
"default": "./dist/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
32
41
|
},
|
|
33
42
|
"scripts": {
|
|
34
|
-
"
|
|
43
|
+
"dev": "tsdx dev",
|
|
35
44
|
"build": "tsdx build",
|
|
36
45
|
"test": "tsdx test",
|
|
37
46
|
"lint": "tsdx lint",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
47
|
+
"format": "tsdx format",
|
|
48
|
+
"typecheck": "tsdx typecheck",
|
|
49
|
+
"prepublishOnly": "bun run build"
|
|
41
50
|
},
|
|
42
|
-
"peerDependencies": {},
|
|
43
|
-
"husky": {
|
|
44
|
-
"hooks": {
|
|
45
|
-
"pre-commit": "tsdx lint"
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
"module": "dist/google-sheets-mapper.esm.js",
|
|
49
|
-
"size-limit": [
|
|
50
|
-
{
|
|
51
|
-
"path": "dist/google-sheets-mapper.cjs.production.min.js",
|
|
52
|
-
"limit": "10 KB"
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
"path": "dist/google-sheets-mapper.esm.js",
|
|
56
|
-
"limit": "10 KB"
|
|
57
|
-
}
|
|
58
|
-
],
|
|
59
51
|
"devDependencies": {
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"tsdx": "^0.
|
|
64
|
-
"
|
|
65
|
-
"
|
|
52
|
+
"bunchee": "^6.9.4",
|
|
53
|
+
"oxfmt": "^0.27.0",
|
|
54
|
+
"oxlint": "^1.42.0",
|
|
55
|
+
"tsdx": "^2.0.0",
|
|
56
|
+
"typescript": "^5.9.3",
|
|
57
|
+
"vitest": "^4.0.18"
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">=20"
|
|
66
61
|
}
|
|
67
62
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
|
-
import { MapperOptions, MapperState, ValueRangesResponse } from
|
|
2
|
-
import { fetchBatchData, fetchAllSheetsData, mapData } from
|
|
1
|
+
import type { MapperOptions, MapperState, ValueRangesResponse } from "./types";
|
|
2
|
+
import { fetchBatchData, fetchAllSheetsData, mapData } from "./utils";
|
|
3
3
|
|
|
4
4
|
const GoogleSheetsMapper = {
|
|
5
5
|
async fetchGoogleSheetsData({
|
|
6
6
|
apiKey,
|
|
7
7
|
sheetId,
|
|
8
|
-
|
|
8
|
+
sheetsOptions = [],
|
|
9
9
|
}: MapperOptions): Promise<MapperState[]> {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return mapData(response.valueRanges);
|
|
17
|
-
} catch (error) {
|
|
18
|
-
throw error;
|
|
19
|
-
}
|
|
10
|
+
const response: ValueRangesResponse =
|
|
11
|
+
sheetsOptions.length === 0
|
|
12
|
+
? await fetchAllSheetsData({ apiKey, sheetId })
|
|
13
|
+
: await fetchBatchData({ apiKey, sheetId, sheetsOptions });
|
|
14
|
+
return mapData({ sheets: response.valueRanges, sheetsOptions });
|
|
20
15
|
},
|
|
21
16
|
};
|
|
22
17
|
|
package/src/types.ts
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
|
+
export interface SheetsOption {
|
|
2
|
+
id: string;
|
|
3
|
+
headerRowIndex?: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
1
6
|
export interface MapperOptions {
|
|
2
7
|
apiKey: string;
|
|
3
8
|
sheetId: string;
|
|
4
|
-
|
|
9
|
+
sheetsOptions?: SheetsOption[];
|
|
5
10
|
}
|
|
6
11
|
|
|
7
12
|
export interface ValueRange {
|
|
8
13
|
majorDimensions: string;
|
|
9
14
|
range: string;
|
|
10
|
-
values:
|
|
15
|
+
values: string[][];
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
export interface ValueRangesResponse {
|
|
14
19
|
spreadsheetId: string;
|
|
15
|
-
valueRanges:
|
|
20
|
+
valueRanges: ValueRange[];
|
|
16
21
|
}
|
|
17
22
|
|
|
18
23
|
export interface PropertiesFromResponse {
|
|
@@ -29,7 +34,7 @@ export interface SheetsResponse {
|
|
|
29
34
|
|
|
30
35
|
export interface MapperState {
|
|
31
36
|
id: string;
|
|
32
|
-
data:
|
|
37
|
+
data: Record<string, string>[];
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
export interface ApiResponse {
|