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

how to make all cells of a given column have the same type? #1882

Closed
sibelius opened this issue Mar 26, 2020 · 1 comment
Closed

how to make all cells of a given column have the same type? #1882

sibelius opened this issue Mar 26, 2020 · 1 comment

Comments

@sibelius
Copy link

I'm following this code #885 (comment)

My usecase is to generate a template excel with only headers to be used as an import for new users

I'd like to have some collums to be of type Date, so they will properly parsed after user fill the template

this is my current approach:

  • generate sheet from given json, we don't have any data, just header
const ws = XLSX.utils.json_to_sheet([], { header: Object.values(columns) });
  • generate same cells in given collums with proper type (1000 rows with Date type)
const DATATYPE_ROWS = 1000;

export const getCellsWithDataTypes = (v, dataTypes) => {
  const column = v[0];

  // generate from 2 to 1000
  const wsData = [...Array(DATATYPE_ROWS).keys()].reduce((acc, i) => {
    const key = `${column}${2 + i}`;

    return {
      ...acc,
      [key]: {
        t: dataTypes[v],
        v: ' ',
      },
    };
  }, {});

  return wsData;
};

I've also tried to use the code from #885 (comment)

export const formatRange = (ws, C: string, fmt: string) => {
  const range = XLSX.utils.decode_range(ws['!ref']);

  for (let i = range.s.r + 1; i <= range.e.r; ++i) {
    /* find the data cell (range.s.r + 1 skips the header row of the worksheet) */
    const ref = XLSX.utils.encode_cell({ r: i, c: C });

    /* if the particular row did not contain data for the column, the cell will not be generated */
    if (!ws[ref]) continue;
    /* `.t == "n"` for number cells */
    if (ws[ref].t !== 'n') continue;
    /* assign the `.z` number format */
    ws[ref].z = fmt;
  }
};

neither of them worked well

I think I'm missing to modify the !ref

@SheetJSDev
Copy link
Contributor

Please follow #1339 for more updates

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

No branches or pull requests

2 participants