Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 08:35:59 +08:00
commit 90883a4d25
287 changed files with 75058 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
#!/usr/bin/env node
/**
* Create Google Sheet
*
* Usage: node create_sheet.js <account> --title <title> [--sheets <sheet1,sheet2>]
*
* Examples:
* node create_sheet.js psd --title "Budget 2025"
* node create_sheet.js psd --title "Project Tracker" --sheets "Tasks,Timeline,Resources"
*/
const { google } = require('googleapis');
const path = require('path');
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
async function createSheet(account, options) {
const auth = await getAuthClient(account);
const sheets = google.sheets({ version: 'v4', auth });
const requestBody = {
properties: {
title: options.title,
},
};
// Add custom sheets if specified
if (options.sheets) {
const sheetNames = options.sheets.split(',').map(s => s.trim());
requestBody.sheets = sheetNames.map(name => ({
properties: { title: name },
}));
}
const response = await sheets.spreadsheets.create({
requestBody,
});
return {
success: true,
account,
spreadsheet: {
id: response.data.spreadsheetId,
title: response.data.properties.title,
url: response.data.spreadsheetUrl,
sheets: response.data.sheets.map(s => s.properties.title),
},
metadata: {
timestamp: new Date().toISOString(),
}
};
}
// CLI interface
async function main() {
const args = process.argv.slice(2);
const account = args[0];
if (!account) {
console.error(JSON.stringify({
error: 'Missing account',
usage: 'node create_sheet.js <account> --title <title> [--sheets <sheet1,sheet2>]'
}));
process.exit(1);
}
// Parse options
const options = {};
for (let i = 1; i < args.length; i++) {
switch (args[i]) {
case '--title':
options.title = args[++i];
break;
case '--sheets':
options.sheets = args[++i];
break;
}
}
if (!options.title) {
console.error(JSON.stringify({
error: 'Missing --title option',
usage: 'node create_sheet.js <account> --title <title> [--sheets <sheet1,sheet2>]'
}));
process.exit(1);
}
try {
const result = await createSheet(account, options);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error(JSON.stringify({
error: error.message,
account,
}));
process.exit(1);
}
}
main();
module.exports = { createSheet };

View File

@@ -0,0 +1,99 @@
#!/usr/bin/env node
/**
* Edit Google Sheet
*
* Usage: node edit_sheet.js <account> <spreadsheet-id> --range <range> --values <json-array>
*
* Examples:
* node edit_sheet.js psd SHEET_ID --range "A1" --values '[["Hello"]]'
* node edit_sheet.js psd SHEET_ID --range "A1:B2" --values '[["Name","Score"],["Alice",100]]'
* node edit_sheet.js psd SHEET_ID --range "Sheet2!A1" --values '[["Data"]]'
*/
const { google } = require('googleapis');
const path = require('path');
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
async function editSheet(account, spreadsheetId, options) {
const auth = await getAuthClient(account);
const sheets = google.sheets({ version: 'v4', auth });
const values = JSON.parse(options.values);
const response = await sheets.spreadsheets.values.update({
spreadsheetId,
range: options.range,
valueInputOption: 'USER_ENTERED',
requestBody: {
values,
},
});
return {
success: true,
account,
spreadsheetId,
update: {
range: response.data.updatedRange,
rowsUpdated: response.data.updatedRows,
columnsUpdated: response.data.updatedColumns,
cellsUpdated: response.data.updatedCells,
},
metadata: {
timestamp: new Date().toISOString(),
}
};
}
// CLI interface
async function main() {
const args = process.argv.slice(2);
const account = args[0];
const spreadsheetId = args[1];
if (!account || !spreadsheetId) {
console.error(JSON.stringify({
error: 'Missing arguments',
usage: 'node edit_sheet.js <account> <spreadsheet-id> --range <range> --values <json-array>'
}));
process.exit(1);
}
// Parse options
const options = {};
for (let i = 2; i < args.length; i++) {
switch (args[i]) {
case '--range':
options.range = args[++i];
break;
case '--values':
options.values = args[++i];
break;
}
}
if (!options.range || !options.values) {
console.error(JSON.stringify({
error: 'Missing --range or --values option',
usage: 'node edit_sheet.js <account> <spreadsheet-id> --range <range> --values <json-array>'
}));
process.exit(1);
}
try {
const result = await editSheet(account, spreadsheetId, options);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error(JSON.stringify({
error: error.message,
account,
spreadsheetId,
}));
process.exit(1);
}
}
main();
module.exports = { editSheet };

View File

@@ -0,0 +1,89 @@
#!/usr/bin/env node
/**
* Read Google Sheet Data
*
* Usage: node read_sheet.js <account> <spreadsheet-id> [--range <range>]
*
* Examples:
* node read_sheet.js psd 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms
* node read_sheet.js psd SHEET_ID --range "Sheet1!A1:D10"
*/
const { google } = require('googleapis');
const path = require('path');
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
async function readSheet(account, spreadsheetId, options = {}) {
const auth = await getAuthClient(account);
const sheets = google.sheets({ version: 'v4', auth });
// Get spreadsheet metadata
const metadata = await sheets.spreadsheets.get({
spreadsheetId,
});
// Get values
const range = options.range || metadata.data.sheets[0].properties.title;
const response = await sheets.spreadsheets.values.get({
spreadsheetId,
range,
});
return {
success: true,
account,
spreadsheet: {
id: spreadsheetId,
title: metadata.data.properties.title,
sheets: metadata.data.sheets.map(s => s.properties.title),
},
data: {
range: response.data.range,
values: response.data.values || [],
rowCount: (response.data.values || []).length,
},
metadata: {
timestamp: new Date().toISOString(),
}
};
}
// CLI interface
async function main() {
const args = process.argv.slice(2);
const account = args[0];
const spreadsheetId = args[1];
if (!account || !spreadsheetId) {
console.error(JSON.stringify({
error: 'Missing arguments',
usage: 'node read_sheet.js <account> <spreadsheet-id> [--range <range>]'
}));
process.exit(1);
}
// Parse options
const options = {};
for (let i = 2; i < args.length; i++) {
if (args[i] === '--range') {
options.range = args[++i];
}
}
try {
const result = await readSheet(account, spreadsheetId, options);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error(JSON.stringify({
error: error.message,
account,
spreadsheetId,
}));
process.exit(1);
}
}
main();
module.exports = { readSheet };