Open mtuchi opened 7 months ago
The upsertValues
function. Will remove the following anti-pattern
getValues(
'18129',
'mtuchi-testing!A:AD',
state => {
const {
taskId,
data: { values },
} = state;
const rowIndex = values.findIndex(row => row['0'] === taskId);
if (rowIndex !== -1) {
state.updateRange = `mtuchi-testing!A${rowIndex + 1}:AD${rowIndex + 1}`;
}
return state;
}
);
fn(state => {
if (state.updateRange) {
console.log('Updating row', state.updateRange);
return batchUpdateValues({
spreadsheetId: '18129',
range: state.updateRange,
values: state.gsheetValues,
})(state);
}
console.log('Adding new row');
return appendValues({
spreadsheetId: '18129',
range: 'mtuchi-testing!A2:AD2',
values: state.gsheetValues,
})(state);
});
@mtuchi I know you came up with the appscript option... so what would next steps be for upsertValues()
?
If there is still work to do, do you think Hunter could pick this up and finish it?
@aleksa-krolls i think upsertValues()
is the hard problem to solve. And might need Joe's input on designing the function
@mtuchi ok then will add to backlog. Lower priority rn.
Hi @mtuchi
I probably need to spend a bit more time with the adaptor before I weigh in on this. Maybe I can do that tomorrow.
I am worried that if you add this, the API as a whole gets a bit confusing? My gut feeling is that its hard to understand for new users.
The questions I'm asking myself are:
append
, batchUpdate AND
upsert`? That's three functions doing every similar things in a small API.appendOrUpdate
? Like append to the end of the range if it doesn't exist, or update the row if it does.I guess I'm just worried that it's overly specific.
What if you restructured your job example something like this? One of the two operations will always be a no-op (it won't do anything)
getValues(
'18129',
'mtuchi-testing!A:AD',
state => {
const {
taskId,
data: { values },
} = state;
const rowIndex = values.findIndex(row => row['0'] === taskId);
if (rowIndex !== -1) {
state.updateRange = `mtuchi-testing!A${rowIndex + 1}:AD${rowIndex + 1}`;
state.toAppend = state.gsheetValues;
state.toUpsert = [];
} else {
state.updateRange = 'mtuchi-testing!A2:AD2';
state.toAppend= [];
state.toUpsert= state.gsheetValues;
}
return state;
}
);
batchUpdateValues({
spreadsheetId: '18129',
range: $.updateRange,
values: $.toUpdate,
})
appendValues({
spreadsheetId: '18129',
range: $.updateRange,
values: $.toAppend
})
Maybe another approach is just to expose the gsheets client directly? A bit like we do with get. It's kinda crude but looking at the adaptor, all it does is proxy to the client anyway
useSheets((state, sheetsClient) => {
// do whatever you want with the client
const response = await sheetsClient.values.batchUpdate({
spreadsheetId: 'abc',
resource: { data: [{ range, values }]
});
// return a new state object
return { ...state, data: response.data }
})
Here's the plan after a phone call with @mtuchi:
upsertValues
(conceptually messy, doesn't map to the google API directly, bloats the API). Let's wait and see if anyone else needs itfnIf()
would help us here so I'm linking this as evidence in support of #375@aleksa-krolls See joe's feedback above βπ½
Description
In some cases you want to update values in a matched row or create a new row. The hardest part is figuring out the range for a matched row that you want to update. A function like
upsertRowValues
orupsertValues
can be really useful and make job code look simple and clean.Suggestions
β οΈ The suggested implementation ππ½ , needs lots of improvements