optilude / xlsx-template

A NodeJS module to generate Excel files in .xlsx format from a template created with Excel itself
MIT License
401 stars 147 forks source link

Cell format not applied. #182

Open serfend opened 1 year ago

serfend commented 1 year ago

while put value 2023-08-09 14:44:32 into template ${datetime} on which cell format YY-MM-DD, you will get 2023-08-09 14:44:32 instead of 2023-08-09

ruanitto commented 8 months ago

Transform your date to a JS date, like README file: https://github.com/optilude/xlsx-template?tab=readme-ov-file#generating-reports

satyajitnayk commented 7 months ago

It is not working.

ruanitto commented 7 months ago

It is not working.

Could you create a repository with your example of use? Maybe by visualizing I can help you.

satyajitnayk commented 7 months ago

template.xlsx

import XlsxTemplate from 'xlsx-template';
import fs from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';

// Determine the directory name in an ES module
const __dirname = dirname(fileURLToPath(import.meta.url));
// Load an XLSX file into memory
fs.readFile(path.join(__dirname, '/', 'template.xlsx'), function (err, data) {
  if (err) {
    throw err; // Handle errors here if the file cannot be read
  }

  // Create a template
  var template = new XlsxTemplate(data);

  // Replacements take place on first sheet
  var sheetNumber = 1;

  // Set up some placeholder values matching the placeholders in the template
  var values = {
    extractDate: new Date('2013-06-01'),
  };

  // Perform substitution
  template.substitute(sheetNumber, values);

  // Get binary data
  var newData = template.generate({ type: 'nodebuffer' });

  // Write the new data to a file
  fs.writeFile(
    path.join(__dirname, '/', 'output.xlsx'),
    newData,
    function (err) {
      if (err) {
        throw err; // Handle the error here
      }
      console.log('The file has been saved!');
    }
  );
});

I have formatted the cell for date

image

But the output excel file does not have formatted date

image

I need to do double click + Enter on the cell to get expected format as below

image

same issue as this

satyajitnayk commented 7 months ago

It is not working.

ignore my previous comments. It's working when using js Date instead of plain string.