@robert.tools/google 1.0.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/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +78 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 robert.tools
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🎯 parse the Google Visualization JSONP response
|
|
3
|
+
* @param {string} text the JSONP response text
|
|
4
|
+
* @returns {object} the parsed JSON object
|
|
5
|
+
*/
|
|
6
|
+
export declare const parseGoogleVisualizationJson: (text: string) => any;
|
|
7
|
+
/**
|
|
8
|
+
* 🎯 get the value of a cell, handling null and undefined values
|
|
9
|
+
* @param {*} cell the cell object
|
|
10
|
+
* @returns {string} the cell value
|
|
11
|
+
*/
|
|
12
|
+
export declare const getCellValue: (cell: any) => any;
|
|
13
|
+
export declare const rowToValues: (row: any) => any;
|
|
14
|
+
export declare const detectColumnsFromHeader: (rows: any[]) => {
|
|
15
|
+
keyColumn: number;
|
|
16
|
+
contextColumn: number;
|
|
17
|
+
};
|
|
18
|
+
export declare const extractSheetData: (sheetJson: any) => {
|
|
19
|
+
matchers: {
|
|
20
|
+
key: string;
|
|
21
|
+
context: any;
|
|
22
|
+
}[];
|
|
23
|
+
keyColumn: number;
|
|
24
|
+
contextColumn: number;
|
|
25
|
+
};
|
|
26
|
+
export declare const getSheetData: (id: string, tab: string) => any;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSheetData = exports.extractSheetData = exports.detectColumnsFromHeader = exports.rowToValues = exports.getCellValue = exports.parseGoogleVisualizationJson = void 0;
|
|
4
|
+
const cmd_1 = require("@robert.tools/cmd");
|
|
5
|
+
const convert_1 = require("@robert.tools/convert");
|
|
6
|
+
const SHEET_URL = 'https://docs.google.com/spreadsheets/d/{id}/gviz/tq?tqx=out:json&sheet={tab}';
|
|
7
|
+
/**
|
|
8
|
+
* 🎯 parse the Google Visualization JSONP response
|
|
9
|
+
* @param {string} text the JSONP response text
|
|
10
|
+
* @returns {object} the parsed JSON object
|
|
11
|
+
*/
|
|
12
|
+
const parseGoogleVisualizationJson = (text) => {
|
|
13
|
+
const match = String(text || '').match(/google\.visualization\.Query\.setResponse\((.*)\);?\s*$/s);
|
|
14
|
+
if (!match) {
|
|
15
|
+
throw new Error('Could not parse Google Sheets response');
|
|
16
|
+
}
|
|
17
|
+
return JSON.parse(match[1]);
|
|
18
|
+
};
|
|
19
|
+
exports.parseGoogleVisualizationJson = parseGoogleVisualizationJson;
|
|
20
|
+
/**
|
|
21
|
+
* 🎯 get the value of a cell, handling null and undefined values
|
|
22
|
+
* @param {*} cell the cell object
|
|
23
|
+
* @returns {string} the cell value
|
|
24
|
+
*/
|
|
25
|
+
const getCellValue = (cell) => {
|
|
26
|
+
return cell?.v ?? '';
|
|
27
|
+
};
|
|
28
|
+
exports.getCellValue = getCellValue;
|
|
29
|
+
const rowToValues = (row) => {
|
|
30
|
+
return (row?.c || []).map((cell) => String((0, exports.getCellValue)(cell) || '').trim());
|
|
31
|
+
};
|
|
32
|
+
exports.rowToValues = rowToValues;
|
|
33
|
+
const detectColumnsFromHeader = (rows) => {
|
|
34
|
+
const firstRow = rows[0];
|
|
35
|
+
const values = (0, exports.rowToValues)(firstRow);
|
|
36
|
+
let keyColumn = -1;
|
|
37
|
+
let contextColumn = -1;
|
|
38
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
39
|
+
const normalized = (0, convert_1.normalizeText)(values[index]);
|
|
40
|
+
if (normalized === 'key') {
|
|
41
|
+
keyColumn = index;
|
|
42
|
+
}
|
|
43
|
+
if (normalized === 'kontext') {
|
|
44
|
+
contextColumn = index;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { keyColumn, contextColumn };
|
|
48
|
+
};
|
|
49
|
+
exports.detectColumnsFromHeader = detectColumnsFromHeader;
|
|
50
|
+
const extractSheetData = (sheetJson) => {
|
|
51
|
+
const rows = sheetJson?.table?.rows || [];
|
|
52
|
+
const { keyColumn, contextColumn } = (0, exports.detectColumnsFromHeader)(rows);
|
|
53
|
+
if (keyColumn === -1 || contextColumn === -1) {
|
|
54
|
+
throw new Error('Could not detect KEY and Kontext columns from sheet header');
|
|
55
|
+
}
|
|
56
|
+
const matchers = [];
|
|
57
|
+
for (const row of rows.slice(1)) {
|
|
58
|
+
const values = (0, exports.rowToValues)(row);
|
|
59
|
+
const key = values[keyColumn] || '';
|
|
60
|
+
const context = values[contextColumn] || '';
|
|
61
|
+
if (!key || !context) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
matchers.push({
|
|
65
|
+
key: (0, convert_1.normalizeText)(key),
|
|
66
|
+
context,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return { matchers, keyColumn, contextColumn };
|
|
70
|
+
};
|
|
71
|
+
exports.extractSheetData = extractSheetData;
|
|
72
|
+
const getSheetData = (id, tab) => {
|
|
73
|
+
const googleSheetUrl = SHEET_URL.replace('{id}', id).replace('{tab}', tab);
|
|
74
|
+
const text = (0, cmd_1.command)(`curl -s "${googleSheetUrl}"`);
|
|
75
|
+
return (0, exports.parseGoogleVisualizationJson)(text);
|
|
76
|
+
};
|
|
77
|
+
exports.getSheetData = getSheetData;
|
|
78
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;AAC5C,mDAAsD;AAEtD,MAAM,SAAS,GACX,8EAA8E,CAAC;AAEnF;;;;GAIG;AACI,MAAM,4BAA4B,GAAG,CAAC,IAAY,EAAE,EAAE;IACzD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,KAAK,CAClC,0DAA0D,CAC7D,CAAC;IACF,IAAI,CAAC,KAAK,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC9D,CAAC;IAED,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAChC,CAAC,CAAC;AATW,QAAA,4BAA4B,gCASvC;AAEF;;;;GAIG;AACI,MAAM,YAAY,GAAG,CAAC,IAAS,EAAE,EAAE;IACtC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
{
|
|
2
|
+
"name": "@robert.tools/google",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "A package to access Google Workspace",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc -p tsconfig.build.json",
|
|
15
|
+
"test": "jest --coverage --runInBand --verbose",
|
|
16
|
+
"lint": "eslint .",
|
|
17
|
+
"check": "npm run lint && npm test && npm run build",
|
|
18
|
+
"prepublishOnly": "npm run check"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/robert-tools/google.git"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/robert-tools/google/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/robert-tools/google#readme",
|
|
28
|
+
"keywords": [
|
|
29
|
+
"typescript",
|
|
30
|
+
"node",
|
|
31
|
+
"google",
|
|
32
|
+
"robert.tools"
|
|
33
|
+
],
|
|
34
|
+
"author": "Robert Willemelis",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=22"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public",
|
|
41
|
+
"registry": "https://registry.npmjs.org/"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/jest": "^29.5.14",
|
|
45
|
+
"@types/node": "^24.0.0",
|
|
46
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
47
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
48
|
+
"eslint": "^9.0.0",
|
|
49
|
+
"eslint-config-prettier": "^10.1.8",
|
|
50
|
+
"eslint-plugin-prettier": "^5.5.6",
|
|
51
|
+
"jest": "^29.7.0",
|
|
52
|
+
"jest-environment-jsdom": "^29.6.2",
|
|
53
|
+
"ts-jest": "^29.4.0",
|
|
54
|
+
"typescript": "^5.9.0"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@robert.tools/cmd": "^1.0.0",
|
|
58
|
+
"@robert.tools/convert": "^1.0.0"
|
|
59
|
+
}
|
|
60
|
+
}
|