Comments (2)
@steevepay Thank you very much for the explanation.
from carbone.
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.
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:
Reach me if you need help.
EDIT: Unfortunately, it's not working with Excel templates.
from carbone.
Related Issues (20)
- [Bug Report]: ifEQ:showBegin doesn't work under some conditions HOT 2
- carbone api HOT 1
- License confirmation HOT 1
- [Bug Report]: Convert Docx to PDF becomes unresponsive HOT 1
- [Feature Request]: Snippet to include
- [Bug Report]: Can't generate PDF with next.js HOT 1
- Minimum requirements HOT 1
- [Feature Request]: ⚡️ Secret news of 14th Februrary 2023: we are building our own PDF converter, x200 faster than LibreOffice! HOT 2
- [Bug Report]: drop(p) on list items not deleting the content HOT 1
- [Bug Report]: PDF Conversion on Ubuntu Server not working HOT 1
- [Feature Request]: Authentication persistence in a containerized environment. HOT 5
- [Feature Request]: FS permissions for persistent volumes on Kubernetes
- [Bug Report]: Table Styles cleared in documents
- [Feature Request]: Render path retention
- [Bug Report]: 404: "Template not found" HOT 1
- Carbone with docker compose
- [Feature Request]: Populate via rag api
- Can I Repeat Entire Pages? HOT 2
- [Bug Report]: Result of render for word document is corrupted if array tags for different fields are used successively
- [Bug Report]: No file send HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carbone.