gglukmann / use-google-sheets

📝 A React Hook for getting data from Google Sheets API v4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't discard row zero!

fotoflo opened this issue · comments

Row zero may contain the table head, or it may contain data. Please dont discard it. Now I will have to use map-reduce to get the table head out of it. (or make a PR)

What would like to have as header? Index of row?

Hi just seeing this.
Row zero would be the first row... usually that makes the table head. I solved the issue a while ago but, if i recall, if there's a blank cell in the sheet, it would create offset on that row? So we ahve to reduce to get all of the keys in the whole sheet to make the headers.

Hi, first off, im so rude... I must thank you for making this awesome library... really helpful!

Here's how im making sure i can get all the headers (for use with react-table)

import { ROWS_TO_GLEAN_HEADERS_FROM } from "next.config";
    // moved the const to config to make it easier to remember there's a bug laying in wait

// go through the dataset and make sure we have all the row headers
export const getItemsHeaders = (tableData) => {
  if (typeof tableData === "undefined") return;

  const headerSet = new Set(); // Collapse a Set to get the unique items
  tableData.some((cell, index) => {
    // just go through the first n rows
    if (index > ROWS_TO_GLEAN_HEADERS_FROM) return; // NOTE: optimization which could cause a bug if the included rows do not contain all the headers
    Object.keys(cell).forEach((key) => headerSet.add(key));
  });

  const headers = Array.from(headerSet); // collapse the set

  return headers.map((header) => {
    return {
      Header: header.toUpperCase(),
      accessor: header,
    };
  });
};