OpenFn / adaptors

The new home for OpenFn adaptors; re-usable connectors for the most common DPGs and DPI building blocks.
GNU General Public License v3.0
7 stars 8 forks source link

`googlesheets`: don't run update or append if there is no data #576

Open josephjclark opened 4 months ago

josephjclark commented 4 months ago

It might be useful sometimes to use the write API in a no-op way. This is quite functional programming-ish and would align well with our job code - you can safely pass no data into appendValues and it'll happily do nothing. This saves us an if statement.

If the values to appendValues or batchUpdateValues are nullish or empty, we should not call out the google API and return early. Maybe we should log a message saying skipping appendValues as no data passed or something.

Example job:

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
})