examples/excel2json/excel2json.js
// do export for excel to json output or data json object
do export for excel to json output or data json object
do export for excel to json output or data json object
function _internalExport() {
  const NAME = process.env.APP_NAME || "excel2json";
  console.log("APP NAME =>", NAME, "\n");
  const INPUT_FILE = process.env.INPUT_FILE || "./data/people.xlsx";
  const OUTPUT_PATH = process.env.OUTPUT_PATH || "./data/outputs";
  const MAPPER_FILE = process.env.MAPPER_FILE || "./data/mapper.json";
  const SHEET_NAME = process.env.SHEET_NAME || "Sheet1";
  const ENCODING = process.env.ENCODING || "utf-8";
  var mapperString = undefined;
  try {
    mapperString = fs.readFileSync(
      MAPPER_FILE,
      { encoding: ENCODING },
      (err) => {
        if (err) {
          console.error(err);
        }
        console.log("Load file suceed =>", MAPPER_FILE);
      }
    );
  } catch (err) {
    console.error("read file error", err);
  }
  const mapperJson = mapperString ? JSON.parse(mapperString) : {};
  const configs = mapperJson.configs || {};
  const columsData = mapperJson.data || undefined;
  const wb = XLSX.readFile(configs.inputFile || INPUT_FILE);
  const xlData = XLSX.utils.sheet_to_json(
    wb.Sheets[configs.sheetName || SHEET_NAME]
  );
  const data = xlData.map((row) => {
    if (columsData == undefined || !columsData) {
      return row;
    }
    var _r = {};
    columsData.map((col) => {
      _r[col.label] = row[col.dataIndex];
    });
    return _r;
  });
  const outputPath = configs.outputPath || OUTPUT_PATH;
  if (!fs.existsSync(outputPath)) {
    fs.mkdirSync(outputPath, { recursive: true });
  }
  const saveToOutput = configs.saveToOutput || true;
  if (saveToOutput) {
    const jsonStringData = JSON.stringify(data);
    try {
      fs.writeFileSync(
        `${outputPath}/${
          configs.outputName || "exported"
        }_${new Date().getTime()}.json`,
        jsonStringData,
        (err) => {
          if (err) throw err;
          console.log("Write succeed!");
        }
      );
    } catch (err) {}
  }
  return data;
}
function _internalExport ( ) { const NAME = process . env . APP_NAME || "excel2json" ; console . log ( "APP NAME =>" , NAME , "\n" ) ; const INPUT_FILE = process . env . INPUT_FILE || "./data/people.xlsx" ; const OUTPUT_PATH = process . env . OUTPUT_PATH || "./data/outputs" ; const MAPPER_FILE = process . env . MAPPER_FILE || "./data/mapper.json" ; const SHEET_NAME = process . env . SHEET_NAME || "Sheet1" ; const ENCODING = process . env . ENCODING || "utf-8" ; var mapperString = undefined ; try { mapperString = fs . readFileSync ( MAPPER_FILE , { encoding : ENCODING } , ( err ) => { if ( err ) { console . error ( err ) ; } console . log ( "Load file suceed =>" , MAPPER_FILE ) ; } ) ; } catch ( err ) { console . error ( "read file error" , err ) ; } const mapperJson = mapperString ? JSON . parse ( mapperString ) : { } ; const configs = mapperJson . configs || { } ; const columsData = mapperJson . data || undefined ; const wb = XLSX . readFile ( configs . inputFile || INPUT_FILE ) ; const xlData = XLSX . utils . sheet_to_json ( wb . Sheets [ configs . sheetName || SHEET_NAME ] ) ; const data = xlData . map ( ( row ) => { if ( columsData == undefined || ! columsData ) { return row ; } var _r = { } ; columsData . map ( ( col ) => { _r [ col . label ] = row [ col . dataIndex ] ; } ) ; return _r ; } ) ; const outputPath = configs . outputPath || OUTPUT_PATH ; if ( ! fs . existsSync ( outputPath ) ) { fs . mkdirSync ( outputPath , { recursive : true } ) ; } const saveToOutput = configs . saveToOutput || true ; if ( saveToOutput ) { const jsonStringData = JSON . stringify ( data ) ; try { fs . writeFileSync ( ` ${ outputPath } ${ configs . outputName || "exported" } ${ new Date ( ) . getTime ( ) } ` , jsonStringData , ( err ) => { if ( err ) throw err ; console . log ( "Write succeed!" ) ; } ) ; } catch ( err ) { } } return data ; }
do export for excel to json output or data json object
do export for excel to json output or data json object
// load from env // get mapper in string // convert mapper from string to json object // read workbook from excel file // mapping the data from read excel file // parse a new path // check directory and create it if not exist // able to save to output or not (default is true) // json data output // write to file