pashto-dictionary/functions/lib/spreadsheet-tools.ts

262 lines
7.1 KiB
TypeScript
Raw Normal View History

2024-11-26 09:00:46 +00:00
import { Types as T } from "@lingdocs/inflect";
2024-11-27 17:27:59 +00:00
import * as FT from "../../website/src/types/functions-types";
2024-11-26 09:00:46 +00:00
import { standardizeEntry } from "@lingdocs/inflect";
2024-11-27 17:27:59 +00:00
import type { sheets_v4 } from "@googleapis/sheets";
2024-11-26 09:00:46 +00:00
import {
dictionaryEntryBooleanFields,
dictionaryEntryNumberFields,
dictionaryEntryTextFields,
2024-11-27 17:27:59 +00:00
simplifyPhonetics,
standardizePashto,
2024-11-26 09:00:46 +00:00
} from "@lingdocs/inflect";
const validFields = [
...dictionaryEntryTextFields,
...dictionaryEntryBooleanFields,
...dictionaryEntryNumberFields,
];
2024-11-27 17:27:59 +00:00
export type Sheets = {
spreadsheetId: string;
spreadsheets: sheets_v4.Resource$Spreadsheets;
};
2024-11-26 09:00:46 +00:00
2024-11-27 17:27:59 +00:00
async function getTsIndex(sheets: Sheets): Promise<number[]> {
const values = await getRange(sheets, "A2:A");
2024-11-26 09:00:46 +00:00
return values.map((r) => parseInt(r[0]));
}
2024-11-27 17:27:59 +00:00
async function getFirstEmptyRow(sheets: Sheets): Promise<number> {
const values = await getRange(sheets, "A2:A");
2024-11-26 09:00:46 +00:00
return values.length + 2;
}
2024-11-27 17:27:59 +00:00
export async function getEntriesFromSheet({
spreadsheets,
spreadsheetId,
}: Sheets): Promise<T.DictionaryEntry[]> {
const keyInfo = await getKeyInfo({ spreadsheets, spreadsheetId });
const { data } = await spreadsheets.values.get({
spreadsheetId,
range: `A2:${keyInfo.lastCol}`,
});
if (!data.values) {
throw new Error("data not found");
}
function processRow(row: string[]) {
// TODO: optimize this
const processedRow = row.flatMap<
[keyof T.DictionaryEntry, string | boolean | number]
>((x, i) => {
if (x === "") {
return [];
}
const k = keyInfo.keyRow[i];
// @ts-expect-error
if (dictionaryEntryNumberFields.includes(k)) {
return [[k, parseInt(x)]];
}
// @ts-expect-error
if (dictionaryEntryBooleanFields.includes(k)) {
return [[k, x.toLowerCase() === "true"]];
}
return [[k, k.endsWith("p") ? standardizePashto(x.trim()) : x.trim()]];
});
return processedRow;
}
const entries = data.values.map(processRow).map((pr) => {
return Object.fromEntries(pr) as T.DictionaryEntry;
});
entries.sort((a, b) => a.p.localeCompare(b.p, "ps"));
const entriesLength = entries.length;
// add index and g
for (let i = 0; i < entriesLength; i++) {
entries[i].i = i;
entries[i].g = simplifyPhonetics(entries[i].f);
}
return entries;
}
export async function updateDictionaryEntries(
{ spreadsheets, spreadsheetId }: Sheets,
edits: FT.EntryEdit[]
) {
2024-11-26 09:00:46 +00:00
if (edits.length === 0) {
return;
}
const entries = edits.map((e) => e.entry);
2024-11-27 17:27:59 +00:00
const tsIndex = await getTsIndex({ spreadsheets, spreadsheetId });
const { keyRow, lastCol } = await getKeyInfo({ spreadsheets, spreadsheetId });
2024-11-26 09:00:46 +00:00
function entryToRowArray(e: T.DictionaryEntry): any[] {
return keyRow.slice(1).map((k) => e[k] || "");
}
const data = entries.flatMap((entry) => {
const rowNum = getRowNumFromTs(tsIndex, entry.ts);
if (rowNum === undefined) {
console.error(`couldn't find ${entry.ts} ${JSON.stringify(entry)}`);
return [];
}
const values = [entryToRowArray(entry)];
return [
{
2024-11-27 17:27:59 +00:00
q: `B${rowNum}:${lastCol}${rowNum}`,
2024-11-26 09:00:46 +00:00
values,
},
];
});
await spreadsheets.values.batchUpdate({
spreadsheetId,
requestBody: {
data,
valueInputOption: "RAW",
},
});
}
2024-11-27 17:27:59 +00:00
export async function addDictionaryEntries(
{ spreadsheets, spreadsheetId }: Sheets,
additions: FT.NewEntry[]
) {
2024-11-26 09:00:46 +00:00
if (additions.length === 0) {
return;
}
const entries = additions.map((x) => standardizeEntry(x.entry));
2024-11-27 17:27:59 +00:00
const endRow = await getFirstEmptyRow({ spreadsheets, spreadsheetId });
const { keyRow, lastCol } = await getKeyInfo({ spreadsheets, spreadsheetId });
2024-11-26 09:00:46 +00:00
const ts = Date.now();
function entryToRowArray(e: T.DictionaryEntry): any[] {
return keyRow.slice(1).map((k) => e[k] || "");
}
const values = entries.map((entry, i) => [ts + i, ...entryToRowArray(entry)]);
await spreadsheets.values.batchUpdate({
spreadsheetId,
requestBody: {
data: [
{
range: `A${endRow}:${lastCol}${endRow + (values.length - 1)}`,
values,
},
],
valueInputOption: "RAW",
},
});
}
export async function updateDictionaryFields(
2024-11-27 17:27:59 +00:00
{ spreadsheets, spreadsheetId }: Sheets,
2024-11-26 09:00:46 +00:00
edits: { ts: number; col: keyof T.DictionaryEntry; val: any }[]
) {
2024-11-27 17:27:59 +00:00
const tsIndex = await getTsIndex({ spreadsheets, spreadsheetId });
const { colMap } = await getKeyInfo({ spreadsheets, spreadsheetId });
2024-11-26 09:00:46 +00:00
const data = edits.flatMap((edit) => {
const rowNum = getRowNumFromTs(tsIndex, edit.ts);
if (rowNum === undefined) {
console.error(`couldn't find ${edit.ts} ${JSON.stringify(edit)}`);
return [];
}
const col = colMap[edit.col];
return [
{
range: `${col}${rowNum}:${col}${rowNum}`,
values: [[edit.val]],
},
];
});
await spreadsheets.values.batchUpdate({
spreadsheetId,
requestBody: {
data,
valueInputOption: "RAW",
},
});
}
2024-11-27 17:27:59 +00:00
export async function deleteEntry(
{ spreadsheets, spreadsheetId }: Sheets,
sheetId: number,
ed: FT.EntryDeletion
) {
const tsIndex = await getTsIndex({ spreadsheets, spreadsheetId });
2024-11-26 09:00:46 +00:00
const row = getRowNumFromTs(tsIndex, ed.ts);
if (!row) {
console.error(`${ed.ts} not found to do delete`);
return;
}
const requests = [
{
deleteDimension: {
range: {
sheetId,
dimension: "ROWS",
startIndex: row - 1,
endIndex: row,
},
},
},
];
await spreadsheets.batchUpdate({
spreadsheetId,
requestBody: {
requests,
includeSpreadsheetInResponse: false,
responseRanges: [],
},
});
}
function getRowNumFromTs(tsIndex: number[], ts: number): number | undefined {
const res = tsIndex.findIndex((x) => x === ts);
if (res === -1) {
return undefined;
}
return res + 2;
}
2024-11-27 17:27:59 +00:00
async function getKeyInfo(sheets: Sheets): Promise<{
2024-11-26 09:00:46 +00:00
colMap: Record<keyof T.DictionaryEntry, string>;
2024-11-27 17:27:59 +00:00
colMapN: Record<keyof T.DictionaryEntry, number>;
2024-11-26 09:00:46 +00:00
keyRow: (keyof T.DictionaryEntry)[];
lastCol: string;
}> {
2024-11-27 17:27:59 +00:00
const headVals = await getRange(sheets, "A1:1");
2024-11-26 09:00:46 +00:00
const headRow: string[] = headVals[0];
2024-11-27 17:27:59 +00:00
const colMap: Record<any, string> = {};
const colMapN: Record<any, number> = {};
2024-11-26 09:00:46 +00:00
headRow.forEach((c, i) => {
if (validFields.every((v) => c !== v)) {
throw new Error(`Invalid spreadsheet field ${c}`);
}
colMap[c] = getColumnLetters(i);
2024-11-27 17:27:59 +00:00
colMapN[c] = i;
2024-11-26 09:00:46 +00:00
});
return {
colMap: colMap as Record<keyof T.DictionaryEntry, string>,
2024-11-27 17:27:59 +00:00
colMapN: colMapN as Record<keyof T.DictionaryEntry, number>,
2024-11-26 09:00:46 +00:00
keyRow: headRow as (keyof T.DictionaryEntry)[],
lastCol: getColumnLetters(headRow.length - 1),
};
}
2024-11-27 17:27:59 +00:00
async function getRange(
{ spreadsheets, spreadsheetId }: Sheets,
range: string
): Promise<any[][]> {
2024-11-26 09:00:46 +00:00
const { data } = await spreadsheets.values.get({
spreadsheetId,
range,
});
if (!data.values) {
throw new Error("data not found");
}
return data.values;
}
function getColumnLetters(num: number) {
let letters = "";
while (num >= 0) {
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[num % 26] + letters;
num = Math.floor(num / 26) - 1;
}
return letters;
}