Giter VIP home page Giter VIP logo

Comments (2)

JVaghela-Fintech avatar JVaghela-Fintech commented on August 22, 2024 1

@steevepay Thank you very much for the explanation.

from carbone.

steevepay avatar steevepay commented on August 22, 2024

Hi @JVaghela-Fintech,

The behavior of creating dynamic sheets is possible but it's not documented yet.
The use of variables is the only way to generate new sheets on ODS files. Before continuing, checkout documentation about Carbone variables.
You have to define 2 variables on the template:

{#sheet1 = d.fruits[i].name}
{#sheet2 = d.fruits[i+1].name}

then insert on the sheet names navigator the variables:

{$sheet1} // for sheet number 1
{$sheet2}  // for sheet number 2

During the rendering, CarboneJS replace the variables by the repetition markers {d.fruits[i].name}and {d.fruits[i+1].name} to generate new sheets that inherit content from the first sheet.
Following this text is an example of an ODS template, a JSON data, the javascript and a screenshot of the generated document. The goal is to generate sheets with fruit nutritional values.

template.ods:
Screenshot 2019-11-25 at 11 18 05

The JSON data to inject, data.json:

{
  "fruits": [
    {
      "name": "apple",
      "nutrients": [
        {
          "type": "energy",
          "value": 52,
          "unit": "Kcal"
        },
        {
          "type": "Carbohydrates",
          "value": 13.81,
          "unit": "g"
        },
        {
          "type": "Protein",
          "value": 0.26,
          "unit": "g"
        },
        {
          "type": "Fats",
          "value": 0.17,
          "unit": "g"
        },
        {
          "type": "Cholesterol",
          "value": 0,
          "unit": "g"
        },
        {
          "type": "Dietary Fiber",
          "value": 2.4,
          "unit": "g"
        }
      ]
    },
    {
      "name": "banana",
      "nutrients": [
        {
          "type": "energy",
          "value": 89,
          "unit": "Kcal"
        },
        {
          "type": "Carbohydrates",
          "value": 22.84,
          "unit": "g"
        },
        {
          "type": "Protein",
          "value": 1.09,
          "unit": "g"
        },
        {
          "type": "Fats",
          "value": 0.33,
          "unit": "g"
        },
        {
          "type": "Cholesterol",
          "value": 0,
          "unit": "g"
        },
        {
          "type": "Dietary Fiber",
          "value": 2.6,
          "unit": "g"
        }
      ]
    },
    {
      "name": "strawberry",
      "nutrients": [
        {
          "type": "energy",
          "value": 53,
          "unit": "Kcal"
        },
        {
          "type": "Carbohydrates",
          "value": 12.75,
          "unit": "g"
        },
        {
          "type": "Protein",
          "value": 1.11,
          "unit": "g"
        },
        {
          "type": "Fats",
          "value": 0.33,
          "unit": "g"
        },
        {
          "type": "Cholesterol",
          "value": 0,
          "unit": "g"
        },
        {
          "type": "Dietary Fiber",
          "value": 3.3,
          "unit": "g"
        }
      ]
    }
  ]
}

index.js

const fs = require("fs");
const carbone = require("carbone");

// function to generate reports
const generateFileWithCarbone = (templateFileName, buildFileName, data) => {
  carbone.render(templateFileName, data,  async (err, result) => {
    if (err) {
      return console.error(err);
    }
    await fs.writeFileSync(buildFileName, result);
  });
};

// Files
const FILE_INPUT = "./template.ods";
const FILE_OUTPUT = "./result.ods";

// Data to inject
const DATA = require("./data.json");

// Call the function
generateFileWithCarbone(FILE_INPUT, FILE_OUTPUT, DATA);

Finally the generated document. result.ods:
Screenshot 2019-11-25 at 11 22 40

Reach me if you need help.

EDIT: Unfortunately, it's not working with Excel templates.

from carbone.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.