azu / philan.net

Public Donation Management webservice for Philanthropist.
https://philan.net
33 stars 1 forks source link

spreadsheet/add: add is not atomic #28

Open azu opened 3 years ago

azu commented 3 years ago

Currently, use sheets.spreadsheets.batchUpdate. It is not atomic operation. So, It can not write concurrently.

sheets.spreadsheets.values.append is atomic operation.

However, sheets.spreadsheets.values.append can not write with format. It only writes values.


    // append is atomic
    // batchUpdate is not atomic
    // https://groups.google.com/g/google-spreadsheets-api/c/G0sUsBHlaZg
    const foundSheetTitle = foundSheet?.properties?.title!;
    return sheets.spreadsheets.values.append({
        oauth_token: token,
        spreadsheetId: meta.spreadsheetId,
        valueInputOption: 'USER_ENTERED',
        insertDataOption: "INSERT_ROWS",
        range: `${foundSheetTitle}!A:A`,
        requestBody: {
            values: [
                [
                    item.date,
                    item.to,
                    item.amount,
                    item.url,
                    item.memo,
                    JSON.stringify(item.meta ?? {})
                ].map((v) => {
                    if (typeof v === "number") {
                        const shouldTransformCurrency = item.currency.from !== item.currency.to;
                        if (shouldTransformCurrency) {
                            const date = dayjs(item.date).format("YYYY/MM/DD");
                            // price * finance rate
                            const value = `= ${v} * index(GOOGLEFINANCE("CURRENCY:${item.currency.from}${item.currency.to}", "price", "${date}"), 2, 2)`;
                            return value;
                        } else {
                            return v;
                        }
                    }
                    return v;
                })

            ]
        }
    });

This is no problem with the application layer. However, a spreadsheet can not show numbers as currency.

https://stackoverflow.com/questions/42696248/how-to-append-and-format-row-at-same-time-google-sheet-api