Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spreadsheet/add: add is not atomic #28

Open
azu opened this issue Mar 21, 2021 · 0 comments
Open

spreadsheet/add: add is not atomic #28

azu opened this issue Mar 21, 2021 · 0 comments
Labels
help wanted Extra attention is needed Status: Proposal Request for comments

Comments

@azu
Copy link
Owner

azu commented Mar 21, 2021

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

@azu azu added help wanted Extra attention is needed Status: Proposal Request for comments labels Mar 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed Status: Proposal Request for comments
Projects
None yet
Development

No branches or pull requests

1 participant