154 lines
No EOL
3.9 KiB
JavaScript
154 lines
No EOL
3.9 KiB
JavaScript
import { SplidClient } from 'splid-js';
|
|
import {google} from "googleapis";
|
|
|
|
// Emoji name mapping - add your aliases as needed
|
|
const NAME_ALIASES = {
|
|
// 🙃 aliases
|
|
'Vrouba von Vrbicz': '🙃',
|
|
'Vróbič': '🙃',
|
|
'Vrba Vrbička': '🙃',
|
|
'Jirka V.': '🙃',
|
|
|
|
// 👨🏭 aliases
|
|
'Párek z Mourkova': '👨🏭',
|
|
'Marek': '👨🏭',
|
|
'Mourek': '👨🏭',
|
|
'Mourek (jen láhve)': '👨🏭',
|
|
|
|
// 🤒 aliases
|
|
'Kruzík Ruzický': '🤒',
|
|
'Kruzík Ruzík': '🤒',
|
|
|
|
// 🤑 aliases
|
|
'Matuzalém Kremžský': '🤑',
|
|
'Matuzalém I/II.': '🤑',
|
|
'Matúš': '🤑',
|
|
|
|
// 😎 aliases
|
|
'martin': '😎',
|
|
'Martin Brony Veleblil': '😎',
|
|
'Veleblil Bobajz z Broníkova': '😎',
|
|
'Bronos': '😎',
|
|
|
|
// 🤯 aliases
|
|
'ja': '🤯',
|
|
'Kuba': '🤯',
|
|
'Kuba-Buba': '🤯',
|
|
"d'Artakuba Zlominoha": '🤯',
|
|
'Kuba Zlominoha': '🤯',
|
|
|
|
// 😴 aliases
|
|
'Sam': '😴',
|
|
'Ditrpich von Šalina': '😴',
|
|
|
|
// 🍖 aliases
|
|
'Šunka Šunkovič Šunkovský': '🍖',
|
|
'Šunka Pražský': '🍖',
|
|
'Šunkovič': '🍖',
|
|
|
|
// Default for unrecognized aliases
|
|
'default': 'Ostatní'
|
|
};
|
|
|
|
function getNameFromAlias(alias) {
|
|
return NAME_ALIASES[alias.toLowerCase()] || NAME_ALIASES['default'];
|
|
}
|
|
|
|
const spreadsheetId = '1RxeQTnirJILgqXDuvI2RQt9vljn1Jz_JFCzVDRQviIg';
|
|
// Create a reusable sheets API client
|
|
async function createSheetsClient() {
|
|
const auth = new google.auth.GoogleAuth({
|
|
keyFile: 'epic-c381a-c7475e0b43cc.json',
|
|
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
|
});
|
|
|
|
return google.sheets({ version: 'v4', auth });
|
|
}
|
|
|
|
// Get data from Google Sheets
|
|
async function getSheetData(spreadsheetId, range) {
|
|
try {
|
|
const sheets = await createSheetsClient();
|
|
const response = await sheets.spreadsheets.values.get({
|
|
spreadsheetId,
|
|
range
|
|
});
|
|
return response.data.values;
|
|
} catch (err) {
|
|
console.error('Error getting sheet data:', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
// Update data in Google Sheets
|
|
async function updateSheetData(spreadsheetId, range, values) {
|
|
try {
|
|
const sheets = await createSheetsClient();
|
|
const response = await sheets.spreadsheets.values.update({
|
|
spreadsheetId,
|
|
range,
|
|
valueInputOption: 'USER_ENTERED',
|
|
resource: { values },
|
|
});
|
|
return response.data;
|
|
} catch (err) {
|
|
console.error('Error updating sheet data:', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function getSplidData(splidCode) {
|
|
const splid = new SplidClient();
|
|
const groupRes = await splid.group.getByInviteCode(splidCode);
|
|
|
|
const groupId = groupRes.result.objectId;
|
|
|
|
const groupInfo = await splid.groupInfo.getOneByGroup(groupId);
|
|
const members = await splid.person.getAllByGroup(groupId);
|
|
const expensesAndPayments = await splid.entry.getAllByGroup(groupId);
|
|
|
|
const balance = SplidClient.getBalance(
|
|
members,
|
|
expensesAndPayments,
|
|
groupInfo
|
|
);
|
|
const suggestedPayments = SplidClient.getSuggestedPayments(balance);
|
|
|
|
return balance;
|
|
}
|
|
|
|
async function resetTableData() {
|
|
// Define a reasonable number of rows to clear/fill (adjust as needed)
|
|
const rowCount = 100;
|
|
|
|
// Create an array of arrays with "-" for each cell in columns E through L
|
|
const dashData = Array(rowCount).fill().map(() => Array(9).fill("-"));
|
|
|
|
try {
|
|
await updateSheetData(spreadsheetId, 'Splidy!E2:M', dashData);
|
|
} catch (err) {
|
|
console.error('Error resetting table data:', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
|
|
const splidCodes = await getSheetData(spreadsheetId, 'Splidy!C2:C');
|
|
const userTags = await getSheetData(spreadsheetId, 'Splidy!E1:M1');
|
|
|
|
console.log(splidCodes);
|
|
await resetTableData();
|
|
|
|
for (const splidCode of splidCodes) {
|
|
const splidCodeString = splidCode[0];
|
|
if (!splidCodeString) {
|
|
continue;
|
|
}
|
|
console.log(`Processing Splid code: ${splidCodeString}`);
|
|
const balance = await getSplidData(splidCodeString);
|
|
console.log(balance);
|
|
}
|
|
}
|
|
|
|
main().catch(console.error); |