node-google-spreadsheet icon indicating copy to clipboard operation
node-google-spreadsheet copied to clipboard

[Bug] addRows replace heading row when there are hidden columns

Open nktnet1 opened this issue 1 year ago • 4 comments

Below is the code I'm currently using:

const serviceAccountAuth = new JWT({
  email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
  key: process.env.GOOGLE_PRIVATE_KEY,
  scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});

const doc = new GoogleSpreadsheet('<some sheet ID>', serviceAccountAuth);
await doc.loadInfo();

const data = getRowData();

if (data.length > 0) {
  const sheet = doc.sheetsByTitle['some title'];
  await sheet.clear();
  await sheet.setHeaderRow(Object.keys(data[0]));

  // This replaces the heading row if there are hidden columns
  await sheet.addRows(data);
}

This works fine when there are no hidden columns. However, if I were to hide some columns using Google Sheet's GUI interface, the heading gets replaced by the first row when using sheet.addRows(data)

By hidden columns, I mean this:

hide-columns

nktnet1 avatar Oct 05 '24 13:10 nktnet1

hello, @nktnet1 have you found any workaround for this issue?

narendra-paiteq avatar Oct 17 '24 12:10 narendra-paiteq

Not really - other than manually un-hiding the columns, run my script, then hiding them again.

Other alternatives might be to use the official Google API, or for a hacky solution, include the header row as the first array item in addRows.

nktnet1 avatar Oct 18 '24 05:10 nktnet1

I think this may be just how the values based api calls work - which is what the row-based apis use.

theoephraim avatar Jul 11 '25 22:07 theoephraim

Perhaps we could the cell-based methods just for the header row update. Open to PRs!

theoephraim avatar Jul 11 '25 22:07 theoephraim