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

patchRows() - Update index.js #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Dmytro2V
Copy link
Contributor

Added patchRows and patchRow methods.
They are similar to updateRows/updateRow, but patchRow() only updates cells which was changed. So functions in some other cells will be safe. Only drawbacks are - 1) writing is cell by cell 2) making a shallow copy of every row to compare are there any changes.
Probably can be more optimized if somehow to read data from updateFn function. And take only fields to change directly.
But proposed version can be added quite easily without major changes.
For now did only small testing, showing cell patch with formulas kept.

  .from('Transactions')
  .where((row) => row.Business.toLowerCase().includes('starbucks'))
  .patchRows((row) => {
    row.Category = 'Coffee Shops';
  });
  console.log(query.getRows()) ;

Added patchRows and patchRow methods
@Dmytro2V
Copy link
Contributor Author

Started to use it. Some details.

When patching row with data, it works smooth.
When we need to add to a new row, it becomes a little tricky. Suppose, we have formulas in some rows filled up to down. Then api will think, last data row is last row with formulas.

I've found several ways, how to find first row with empty cell in some column (like 'id' column).

  1. We can use formulas, which returns blank when non active. Here I mean like IF(A10 = '';''; A102).
    to return blank we can use like IF(A10 = ''; ; A10
    2).
    In this case api get last row correctly.

  2. We can use array formulas only in a top row, so values will be calculated. This approach allows to use an old update method.

  3. We can store somewhere on the sheet last + 1 row number, like =3 + ROWS( FILTER( A4:A ; NOT( ISBLANK( A4:A ) ) )) + 1.
    In this case we probably need to use idx in (row,idx) => {row.ID = nextRow }

  4. We can try to figure out this row with condition - previous row !== '', current row ===''.
    Can add check next 2-3 rows are also ==='' or undefined (to skip possible holes above the header row).
    We need use (row, idx, arr) ={ .... }.
    Have checked, this works.

Probably the best approach is 3, if 2 is not implemented on a sheet.

@vlucas
Copy link
Owner

vlucas commented Dec 2, 2023

Can you write some tests for this for the behavior that you expect?

I would love to merge this in.

With formulas, you can probably just check if the cell starts with a = and don't count it as having a value if it does.

@glompho
Copy link

glompho commented Dec 16, 2023

Thanks for this very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants