philan.net icon indicating copy to clipboard operation
philan.net copied to clipboard

spreadsheet/add: add is not atomic

Open azu opened this issue 4 years ago • 0 comments

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

sheets.spreadsheets.values.append is atomic operation.

  • https://groups.google.com/g/google-spreadsheets-api/c/G0sUsBHlaZg

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

azu avatar Mar 21 '21 01:03 azu