181 lines
No EOL
4.8 KiB
JavaScript
181 lines
No EOL
4.8 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': '🤒',
|
|
'krouzič': '🤒',
|
|
|
|
// 🤑 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č': '🍖',
|
|
'dan': '🍖',
|
|
|
|
// 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: 'application-key.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
|
|
);
|
|
|
|
// Create a map of member IDs to their names/initials
|
|
const memberMap = {};
|
|
for (const member of members) {
|
|
memberMap[member.GlobalId] = {
|
|
name: member.name,
|
|
emoji: getNameFromAlias(member.name)
|
|
};
|
|
}
|
|
|
|
// Enhance the balance object with member information
|
|
const enhancedBalance = {};
|
|
for (const [memberId, balanceInfo] of Object.entries(balance)) {
|
|
enhancedBalance[memberId] = {
|
|
...balanceInfo,
|
|
memberInfo: memberMap[memberId] || { name: 'Unknown', emoji: NAME_ALIASES['default'] }
|
|
};
|
|
}
|
|
|
|
return enhancedBalance;
|
|
}
|
|
|
|
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'); // Get header row as array
|
|
|
|
console.log(splidCodes);
|
|
await resetTableData();
|
|
|
|
for (let i = 0; i < splidCodes.length; i++) {
|
|
const splidCodeString = splidCodes[i][0];
|
|
if (!splidCodeString) {
|
|
continue;
|
|
}
|
|
console.log(`Processing Splid code: ${splidCodeString}`);
|
|
const balance = await getSplidData(splidCodeString);
|
|
|
|
const rowData = [];
|
|
|
|
for (const tag of userTags[0]) {
|
|
const memberEntry = Object.entries(balance).find(([_, info]) => info.memberInfo.emoji === tag);
|
|
rowData.push(memberEntry ? memberEntry[1].balance : '-');
|
|
}
|
|
|
|
// Update the sheet with the row data
|
|
await updateSheetData(spreadsheetId, `Splidy!E${i+2}:M${i+2}`, [rowData]);
|
|
}
|
|
}
|
|
|
|
main().catch(console.error); |