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

How to check whether headers are present in the sheet or not ?

Open sskanishk opened this issue 2 years ago • 4 comments

This im using to check headers await sheet.loadHeaderRow(1) but giving error - Error: No values in the header row - fill the first row with header values before trying to interact with rows

Is there any way to check ?

sskanishk avatar Jun 27 '22 10:06 sskanishk

Having the same issue. I thought something like this would work:

async function hasHeaders(sheet) {
    try {
        await sheet.loadHeaderRow()
        return true;
    } catch (error) {
        return false;
    }
}

But I guess it's not actually throwing an error because it seems it can't be caught.

mitchelldw01 avatar Nov 03 '22 01:11 mitchelldw01

that's nice hacky way.

sskanishk avatar Nov 03 '22 15:11 sskanishk

is there no alternative way? I'd expect loadRows to allow me to load the first row or even loadCells to allow to load the first cell....

liliarose avatar Mar 12 '24 00:03 liliarose

By default, loadRows just automatically loads the header row for you, since in 90% of use cases, you are working with an existing sheet that already has the header set - or you've just set it on a new sheet and we already know what the headers are. In the case you need more control, that's sort of what sheet.loadHeaderRow is for, but I can see it's a little awkward to have to throw / catch an untyped error.

We could add another option to the loadHeaderRow method, or add another method which loads the headers without throwing... feel free to open a PR, or I'll try to add it in during my next pass of udpates.

theoephraim avatar Mar 12 '24 00:03 theoephraim