### Installing Officegen via npm (Bash) Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-install.md This command installs the officegen library into your project using the npm package manager. It adds officegen as a dependency, making it available for use in your application. ```bash npm install officegen ``` -------------------------------- ### Installing Latest Officegen from Repository via npm (Bash) Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-install.md This command installs the officegen library directly from its master branch on GitHub using npm. This provides the latest, potentially unstable, version of the library and is suitable for testing new features. ```bash npm install Ziv-Barber/officegen#master ``` -------------------------------- ### Installing Officegen via Yarn (Bash) Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-install.md This command installs the officegen library into your project using the Yarn package manager. It adds officegen as a dependency, making it available for use in your application. ```bash yarn add officegen ``` -------------------------------- ### Installing officegen via npm Source: https://github.com/ziv-barber/officegen/blob/master/README.md This command installs the officegen library using npm, the Node.js package manager. It's a prerequisite for using officegen in your project, allowing you to import and utilize its functionalities. ```bash $ npm install officegen ``` -------------------------------- ### Generating JSDoc Documentation with Grunt - Bash Source: https://github.com/ziv-barber/officegen/blob/master/README.md This command executes the 'jsdoc' task defined in the Gruntfile.js, which is responsible for generating API documentation for the project's JavaScript source code. It requires Grunt to be installed and configured with a 'jsdoc' task. ```bash grunt jsdoc ``` -------------------------------- ### Adding Styled Text and Links to Paragraphs in officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This comprehensive example showcases various ways to add text to a paragraph using `pObj.addText()`, applying different formatting options like color, background, bold, underline, font face, font size, and external/internal hyperlinks. It also demonstrates bookmark creation and closure for internal links. ```javascript pObj.addText ( 'Simple' ); pObj.addText ( ' with color', { color: '000088' } ); pObj.addText ( ' and back color.', { color: '00ffff', back: '000088' } ); pObj.addText ( 'Bold + underline', { bold: true, underline: true } ); pObj.addText ( 'Fonts face only.', { font_face: 'Arial' } ); pObj.addText ( ' Fonts face and size. ', { font_face: 'Arial', font_size: 40 } ); pObj.addText ( 'External link', { link: 'https://github.com' } ); // Hyperlinks to bookmarks also supported: pObj.addText ( 'Internal link', { hyperlink: 'myBookmark' } ); // ... // Start somewhere a bookmark: pObj.startBookmark ( 'myBookmark' ); // ... // You MUST close your bookmark: pObj.endBookmark (); ``` -------------------------------- ### Creating PPTX Slides with Built-in Layouts in JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet demonstrates the use of convenience methods to create slides based on common Microsoft Office built-in layouts. `makeTitleSlide` creates a title slide, `makeObjSlide` creates a slide with a title and an object placeholder, and `makeSecHeadSlide` creates a section header slide, simplifying the initial setup of common slide types. ```js let slide = pptx.makeTitleSlide(title, subTitle) let slide = pptx.makeObjSlide(title, objData) let slide = pptx.makeSecHeadSlide(title, subTitle) ``` -------------------------------- ### Streaming PowerPoint Document via HTTP Response (Node.js) Source: https://github.com/ziv-barber/officegen/blob/master/README.md This example illustrates how to create a Node.js HTTP server that dynamically generates a PowerPoint document and streams it directly to the client's browser without saving it to a file. It sets appropriate HTTP headers for content type and disposition, and includes error handling for the 'officegen' process and the HTTP response stream. ```javascript const officegen = require('officegen') const http = require('http') /** * This is a simple web server that response with a PowerPoint document. */ http.createServer(function(req, res) { // We'll send a generated on the fly PowerPoint document without using files: if (req.url == '/') { // Create an empty PowerPoint object: let pptx = officegen('pptx') // Let's create a new slide: var slide = pptx.makeNewSlide() slide.name = 'Hello World' // Change the background color: slide.back = '000000' // Declare the default color to use on this slide: slide.color = 'ffffff' // Basic way to add text string: slide.addText('Created on the fly using a http server!') // // Let's generate the PowerPoint document directly into the response stream: // response.writeHead(200, { 'Content-Type': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Content-disposition': 'attachment filename=out.pptx' }) // Content types related to Office documents: // .xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet // .xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template // .potx application/vnd.openxmlformats-officedocument.presentationml.template // .ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow // .pptx application/vnd.openxmlformats-officedocument.presentationml.presentation // .sldx application/vnd.openxmlformats-officedocument.presentationml.slide // .docx application/vnd.openxmlformats-officedocument.wordprocessingml.document // .dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template // .xlam application/vnd.ms-excel.addin.macroEnabled.12 // .xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12 // This one catch only the officegen errors: pptx.on('error', function(err) { res.end(err) }) // Catch response errors: res.on('error', function(err) { res.end(err) }) // End event after sending the PowerPoint data: res.on('finish', function() { res.end() }) // This async method is working like a pipe - it'll generate the pptx data and pass it directly into the output stream: pptx.generate(res) } else { res.end('Invalid Request!') } // Endif. }).listen(3000) ``` -------------------------------- ### Generating Microsoft Excel Document with Officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/README.md This snippet illustrates how to create an .xlsx file using the officegen library. It involves initializing an Excel workbook object, attaching event listeners, creating a new sheet, and populating cells with data using both setCell and direct array manipulation. The example concludes by showing how to save the generated Excel workbook to a file. ```JavaScript const officegen = require('officegen') const fs = require('fs') // Create an empty Excel object: let xlsx = officegen('xlsx') // Officegen calling this function after finishing to generate the xlsx document: xlsx.on('finalize', function(written) { console.log( 'Finish to create a Microsoft Excel document.' ) }) // Officegen calling this function to report errors: xlsx.on('error', function(err) { console.log(err) }) let sheet = xlsx.makeNewSheet() sheet.name = 'Officegen Excel' // Add data using setCell: sheet.setCell('E7', 42) sheet.setCell('I1', -3) sheet.setCell('I2', 3.141592653589) sheet.setCell('G102', 'Hello World!') // The direct option - two-dimensional array: sheet.data[0] = [] sheet.data[0][0] = 1 sheet.data[1] = [] sheet.data[1][3] = 'some' sheet.data[1][4] = 'data' sheet.data[1][5] = 'goes' sheet.data[1][6] = 'here' sheet.data[2] = [] sheet.data[2][5] = 'more text' sheet.data[2][6] = 900 sheet.data[6] = [] sheet.data[6][2] = 1972 // Let's generate the Excel document into a file: let out = fs.createWriteStream('example.xlsx') out.on('error', function(err) { console.log(err) }) // Async call to generate the output file: xlsx.generate(out) ``` -------------------------------- ### Setting Slide Background Color with Color Object - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This example illustrates setting a slide's background color using a detailed color object. It specifies the 'type' of fill (e.g., 'solid') and the 'color' code, providing more control over the background appearance. ```javascript slide.back = {type: 'solid', color: '008800'}; ``` -------------------------------- ### Adding Text to a Slide with Various Options - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This comprehensive example showcases multiple ways to add text to a slide using the 'addText' method. It covers basic text strings, text positioned with coordinates, full-line text boxes, and complex text blocks with mixed fonts, sizes, and colors. It also demonstrates setting default slide background and text colors. ```javascript // Change the background color: slide.back = '000000'; // Declare the default color to use on this slide (default is black): slide.color = 'ffffff'; // Basic way to add text string: slide.addText('This is a test'); slide.addText('Fast position', 0, 20); slide.addText('Full line', 0, 40, '100%', 20); // Add text box with multi colors and fonts: slide.addText([ {text: 'Hello ', options: {font_size: 56}}, {text: 'World!', options: {font_size: 56, font_face: 'Arial', color: 'ffff00'}} ], {cx: '75%', cy: 66, y: 150}); // Please note that you can pass object as the text parameter to addText. slide.addText('Office generator', { y: 66, x: 'c', cx: '50%', cy: 60, font_size: 48, color: '0000ff' } ); slide.addText('Big Red', { y: 250, x: 10, cx: '70%', font_face: 'Wide Latin', font_size: 54, color: 'cc0000', bold: true, underline: true } ); ``` -------------------------------- ### Creating Nested Lists in officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md These examples demonstrate how to create nested unordered and ordered list items by specifying a `level` option. This allows for hierarchical list structures, where each list item is still managed as a paragraph object. ```javascript pObj = docx.createNestedUnOrderedList({ "level":2 }) pObj = docx.createNestedOrderedList({ "level":2 }) ``` -------------------------------- ### Adding a Page Break in officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This snippet demonstrates how to insert a page break into the document using `docx.putPageBreak()`. This method forces the content that follows to start on a new page, useful for document layout and pagination. ```javascript docx.putPageBreak() ``` -------------------------------- ### Customizing Chart Text Size via XML Override (officegen, JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This example illustrates how to apply custom XML overrides to the `chartInfo` object to control the overall text size within a chart. It shows the underlying XML structure used by `officegen` when `fontSize` is specified. ```javascript chartInfo = { // .... "xml": { "c:txPr": { "a:bodyPr": {}, "a:listStyle": {}, "a:p": { "a:pPr": { "a:defRPr": { "@sz": "1200" } }, "a:endParaRPr": { "@lang": "en-US" } } } } ``` -------------------------------- ### Applying Advanced Text Formatting to PPTX Slide Titles in JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This advanced example demonstrates how to apply rich text formatting to slide titles and subtitles using an array of text objects. Each object in the array can specify text content and `options` for styling, such as `font_size`, `font_face`, and `color`, allowing for granular control over the appearance of text elements within a slide's layout. ```javascript slide = pptx.makeNewSlide({ userLayout: 'title' }); // Both setTitle and setSubTitle excepting all the parameters that you can pass to slide.addText - see below: slide.setTitle([ // This array is like a paragraph and you can use any settings that you pass for creating a paragraph, // Each object here is like a call to addText: { text: 'Hello ', options: {font_size: 56} }, { text: 'World!', options: { font_size: 56, font_face: 'Arial', color: 'ffff00' } } ]); ``` -------------------------------- ### Creating a Basic PowerPoint Document Object (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet shows the simplest way to initialize a new PowerPoint document object using the 'officegen' module. By passing 'pptx' as a string argument, it creates a default PowerPoint presentation instance ready for content. ```JavaScript let pptx = officegen('pptx') ``` -------------------------------- ### Initializing PowerPoint Document with Settings (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet illustrates how to create a PowerPoint document object while providing an options object for initial configuration. The 'type' property is crucial to specify 'pptx' for a PowerPoint document, allowing for additional settings to be passed within the object. ```JavaScript let pptx = officegen({ type: 'pptx', // We want to create a Microsoft Powerpoint document. ... // Extra options goes here. }) ``` -------------------------------- ### Initializing officegen for XLSX with Settings - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/xlsx/README.md This snippet illustrates how to initialize the `officegen` module for creating an XLSX document while passing configuration settings. The `type` property specifies the document format, and additional options can be included. ```JavaScript let xlsx = officegen({ type: 'xlsx', // We want to create a Microsoft Excel document. ... // Extra options goes here. }) ``` -------------------------------- ### Applying a Custom Theme to PowerPoint Document (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet demonstrates how to initialize a PowerPoint document with a custom theme by providing the 'themeXml' option within the configuration object. The theme XML content, typically copied from an existing Office document, allows for consistent branding and styling. ```JavaScript let pptx = officegen({ type: 'pptx', // We want to create a Microsoft Powerpoint document. themeXml: '... theme xml goes here ...' // Just copy the theme xml code from existing office document (stored in ppt\\theme\\theme1.xml). }) ``` -------------------------------- ### Initializing officegen for XLSX - Simple Way - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/xlsx/README.md This snippet shows the simplest method to initialize the `officegen` module to create a new Microsoft Excel (XLSX) document. It directly specifies the document type as a string argument. ```JavaScript let xlsx = officegen('xlsx') ``` -------------------------------- ### Generating Document to File Stream - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet shows how to generate an officegen document (e.g., PPTX) and write it to a local file using Node.js's `fs.createWriteStream`. It also demonstrates listening for the 'close' event on the output stream to confirm successful file creation. ```javascript var out = fs.createWriteStream('out.pptx') out.on('close', function () { console.log('Finished to create the PPTX file!') }) pptx.generate(out) ``` -------------------------------- ### Creating a Basic Table with Dynamic Content (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This JavaScript snippet generates a 12x5 table, populating each cell with its [row,column] index. It then uses `slide.addTable()` to insert this dynamically created table into a PowerPoint slide, demonstrating basic table construction. ```javascript var rows = []; for (var i = 0; i < 12; i++) { var row = []; for (var j = 0; j < 5; j++) { row.push("[" + i + "," + j + "]"); } rows.push(row); } slide.addTable(rows, {}); ``` -------------------------------- ### Initializing Basic DOCX Document - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This code initializes a new Microsoft Word (DOCX) document object using the 'officegen' module with default settings. It creates a basic document instance ready for content addition. ```JavaScript let docx = officegen('docx') ``` -------------------------------- ### Initializing DOCX Document with Custom Settings - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This snippet shows how to initialize a Microsoft Word (DOCX) document with custom settings by passing an options object to the 'officegen' constructor. The 'type' property specifies the document format, and additional options can be included. ```JavaScript let docx = officegen({ type: 'docx', // We want to create a Microsoft Word document. ... // Extra options goes here. }) ``` -------------------------------- ### Instantiating officegen Object - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet illustrates two ways to create an officegen object: a direct call with the document type string or an object literal with a 'type' property. This object serves as the foundation for building Microsoft Office documents. Supported types include 'pptx', 'ppsx', 'docx', and 'xlsx'. ```javascript var myDoc = officegen('') // or: var myDoc = officegen({ 'type': '' // More options here (if needed) }) ``` -------------------------------- ### Handling officegen Errors via onerr Option - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet demonstrates an alternative, older method for handling errors by providing an 'onerr' callback directly within the officegen object's configuration options during instantiation. This callback will be invoked if any errors occur during the document creation. ```javascript var pptx = officegen({ 'type': 'pptx', // or 'xlsx', etc 'onerr': function (err) { console.log(err) } }) ``` -------------------------------- ### Requiring the officegen Module - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet demonstrates how to import the officegen module into a Node.js application, making its functionalities available for document generation. It is the first step required before creating any officegen objects. ```javascript var officegen = require('officegen') ``` -------------------------------- ### Importing officegen Module - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/xlsx/README.md This snippet demonstrates how to import the `officegen` module into a JavaScript project using `require()`. This is the first step to access its functionalities for generating Office documents. ```JavaScript const officegen = require('officegen') ``` -------------------------------- ### Importing the officegen Module (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet demonstrates how to import the 'officegen' module into a JavaScript project using the 'require' function. This is the essential first step to access any of the module's functionalities for generating Office documents. ```JavaScript const officegen = require('officegen') ``` -------------------------------- ### Generating Document for HTTP Response - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet illustrates how to generate an officegen document (e.g., PPTX) and send it directly as an HTTP response, without saving it to a file. It sets appropriate HTTP headers for content type and disposition, making the document available for download in a web browser. ```javascript var http = require('http') var officegen = require('officegen') http.createServer(function (request, response) { response.writeHead (200, { 'Content-Type': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'Content-disposition': 'attachment; filename=surprise.pptx' }) var pptx = officegen('pptx') pptx.on('finalize', function (written) { // We don't really need it in this case. }) pptx.on('error', function (err) { // Error handing... }) // ... (fill pptx with data) // Generate the Powerpoint document and sent it to the client via http: pptx.generate(response) }).listen(3000) ``` -------------------------------- ### Generating PowerPoint File with Officegen (Node.js) Source: https://github.com/ziv-barber/officegen/blob/master/README.md This snippet demonstrates how to create a PowerPoint document (.pptx) using 'officegen', add a title slide and a pie chart slide, and then save the generated document to a local file. It includes error handling for both 'officegen' and the file stream. ```javascript const officegen = require('officegen') const fs = require('fs') // Create an empty PowerPoint object: let pptx = officegen('pptx') // Let's add a title slide: let slide = pptx.makeTitleSlide('Officegen', 'Example to a PowerPoint document') // Pie chart slide example: slide = pptx.makeNewSlide() slide.name = 'Pie Chart slide' slide.back = 'ffff00' slide.addChart( { title: 'My production', renderType: 'pie', data: [ { name: 'Oil', labels: ['Czech Republic', 'Ireland', 'Germany', 'Australia', 'Austria', 'UK', 'Belgium'], values: [301, 201, 165, 139, 128, 99, 60], colors: ['ff0000', '00ff00', '0000ff', 'ffff00', 'ff00ff', '00ffff', '000000'] } ] } ) // Let's generate the PowerPoint document into a file: return new Promise((resolve, reject) => { let out = fs.createWriteStream('example.pptx') // This one catch only the officegen errors: pptx.on('error', function(err) { reject(err) }) // Catch fs errors: out.on('error', function(err) { reject(err) }) // End event after creating the PowerPoint file: out.on('close', function() { resolve() }) // This async method is working like a pipe - it'll generate the pptx data and put it into the output stream: pptx.generate(out) }) ``` -------------------------------- ### Creating PowerPoint Document Object - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet shows how to create an empty officegen object specifically for generating Microsoft PowerPoint (PPTX) documents. This object will be populated with slides and content before generation. ```javascript var pptx = officegen('pptx') ``` -------------------------------- ### Generating Microsoft Word Document with Officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/README.md This snippet demonstrates how to create a .docx file using the officegen library. It covers initializing a Word document object, attaching event listeners for completion and errors, creating paragraphs, adding formatted text (color, background, highlight, links, bold, underline, alignment), inserting line breaks and page breaks, and adding images. Finally, it shows how to save the generated document to a file. ```JavaScript const officegen = require('officegen') const fs = require('fs') // Create an empty Word object: let docx = officegen('docx') // Officegen calling this function after finishing to generate the docx document: docx.on('finalize', function(written) { console.log( 'Finish to create a Microsoft Word document.' ) }) // Officegen calling this function to report errors: docx.on('error', function(err) { console.log(err) }) // Create a new paragraph: let pObj = docx.createP() pObj.addText('Simple') pObj.addText(' with color', { color: '000088' }) pObj.addText(' and back color.', { color: '00ffff', back: '000088' }) pObj = docx.createP() pObj.addText('Since ') pObj.addText('officegen 0.2.12', { back: '00ffff', shdType: 'pct12', shdColor: 'ff0000' }) // Use pattern in the background. pObj.addText(' you can do ') pObj.addText('more cool ', { highlight: true }) // Highlight! pObj.addText('stuff!', { highlight: 'darkGreen' }) // Different highlight color. pObj = docx.createP() pObj.addText('Even add ') pObj.addText('external link', { link: 'https://github.com' }) pObj.addText('!') pObj = docx.createP() pObj.addText('Bold + underline', { bold: true, underline: true }) pObj = docx.createP({ align: 'center' }) pObj.addText('Center this text', { border: 'dotted', borderSize: 12, borderColor: '88CCFF' }) pObj = docx.createP() pObj.options.align = 'right' pObj.addText('Align this text to the right.') pObj = docx.createP() pObj.addText('Those two lines are in the same paragraph,') pObj.addLineBreak() pObj.addText('but they are separated by a line break.') docx.putPageBreak() pObj = docx.createP() pObj.addText('Fonts face only.', { font_face: 'Arial' }) pObj.addText(' Fonts face and size.', { font_face: 'Arial', font_size: 40 }) docx.putPageBreak() pObj = docx.createP() // We can even add images: pObj.addImage('some-image.png') // Let's generate the Word document into a file: let out = fs.createWriteStream('example.docx') out.on('error', function(err) { console.log(err) }) // Async call to generate the output file: docx.generate(out) ``` -------------------------------- ### Activating Verbose Debugging Mode - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet shows how to enable verbose logging for the officegen module. Setting verbose mode to `true` can help in debugging by providing more detailed messages during document generation, though it may not cover all internal library parts. ```javascript officegen.setVerboseMode(true) ``` -------------------------------- ### Creating PPTX Slides with Specific User Layouts in JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This alternative method for creating a new slide allows specifying a `userLayout` option, such as 'title', 'obj', or 'secHead'. After creation, specific methods like `setTitle`, `setSubTitle`, or `setObjData` are used to populate the layout's predefined text or object placeholders, offering more control over layout selection. ```javascript let slide = pptx.makeNewSlide({ userLayout: 'title' }); slide.setTitle('The title'); slide.setSubTitle('Another text'); // For either 'title' and 'secHead' only. // for 'obj' layout use slide.setObjData(...) to change the object element inside the slide. ``` -------------------------------- ### Updating PPTX Document Properties in JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet demonstrates how to dynamically update various document properties such as title, subject, keywords, description, category, and status after the `pptx` object has been initialized. Each method takes a string argument to set the respective property, allowing for post-creation modification of the presentation's metadata. ```js pptx.setDocTitle('...') pptx.setDocSubject('...') pptx.setDocKeywords('...') pptx.setDescription( '...') pptx.setDocCategory('...') pptx.setDocStatus('...') ``` -------------------------------- ### Populating Excel Cells - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/xlsx/README.md This snippet shows two methods for populating cells within an Excel sheet. The `setCell` method allows setting values by cell reference (e.g., 'E7'), while direct array manipulation via `sheet.data` provides a more granular approach for row and column indexing. ```JavaScript // Using setCell: sheet.setCell ( 'E7', 340 ); sheet.setCell ( 'G102', 'Hello World!' ); // Direct way: sheet.data[0] = []; sheet.data[0][0] = 1; sheet.data[0][1] = 2; sheet.data[1] = []; sheet.data[1][3] = 'abc'; ``` -------------------------------- ### Adding Headers and Footers in officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This snippet shows how to add a header to a Word document using `officegen`. It retrieves the header object, which is a paragraph, and then adds text to it. It also notes that `getHeader()` and `getFooter()` can accept parameters for even or first page headers/footers, and currently only paragraph creation is supported. ```JavaScript // Add a header: var header = docx.getHeader ().createP (); header.addText ( 'This is the header' ); // Please note that the object header here is a paragraph object so you can use ANY of the paragraph API methods also for header and footer. // The getHeader () method excepting a string parameter: // getHeader ( 'even' ) - change the header for even pages. // getHeader ( 'first' ) - change the header for the first page only. // to do all of that for the footer, use the getFooter instead of getHeader. // and sorry, right now only createP is supported (so only creating a paragraph) so no tables, etc. ``` -------------------------------- ### Importing officegen Module - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This snippet demonstrates how to import the 'officegen' module into a JavaScript project using the `require` function, making its functionalities available for creating Office documents. ```JavaScript const officegen = require('officegen') ``` -------------------------------- ### Defining Document Content and Generating DOCX with officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This snippet demonstrates how to define a document's content using a JSON-like array structure for the `officegen` library and then generate a DOCX file. The data includes image paths resolved relative to the current directory and a page break. The `docx.createByJson` method processes this data to create the document. Note that the initial `path` property is part of an implied image object. ```JavaScript path: path.resolve(__dirname, 'images_for_examples/sword_001.png') },{ type: "image", path: path.resolve(__dirname, 'images_for_examples/sword_002.png') }], { type: "pagebreak" } ] docx.createByJson(data); ``` -------------------------------- ### Generating Document with Callbacks - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet demonstrates how to use callbacks with the `generate` method for handling document finalization and errors. The 'finalize' callback provides the total bytes written, while the 'error' callback handles any issues during generation, offering an alternative to event listeners. ```javascript var out = fs.createWriteStream('out.pptx') pptx.generate(out, { 'finalize': function (written) { console.log('Finish to create a PowerPoint file.\nTotal bytes created: ' + written + '\n') }, 'error': function (err) { console.log(err) } }) ``` -------------------------------- ### Creating a Line Chart in PowerPoint (officegen, JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet demonstrates how to create a line chart on a new PowerPoint slide. It includes multiple data series representing different regions (europe, namerica, asia) with their respective labels, values, and optional colors. ```javascript // Line Chart slide = pptx.makeNewSlide(); slide.name = 'Line Chart slide'; slide.back = 'ff00ff'; slide.addChart( { title: 'Sample line chart', renderType: 'line', data: [ // each item is one serie { name: 'europe', labels: ['Y2003', 'Y2004', 'Y2005', 'Y2006'], values: [2.5, 2.6, 2.8, 2.4], color: 'ff0000' // optional }, { name: 'namerica', labels: ['Y2003', 'Y2004', 'Y2005', 'Y2006'], values: [2.5, 2.7, 2.9, 3.2], color: '00ff00' // optional }, { name: 'asia', labels: ['Y2003', 'Y2004', 'Y2005', 'Y2006'], values: [2.1, 2.2, 2.4, 2.2], ``` -------------------------------- ### Applying Cell-Specific Formatting in PowerPoint Tables (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet demonstrates advanced table cell formatting using `officegen`. It defines two header cells with distinct styling options like font face, alignment, bolding, font color, and fill color, then adds this pre-formatted row as a table to a slide. ```javascript var rows = []; rows.push([ { val: "Category", opts: { font_face : "Arial", align : "l", bold : 0 } }, { val :"Average Score", opts: { font_face : "Arial", align : "r", bold : 1, font_color : "000000", fill_color : "f5f5f5" } } ]); slide.addTable(rows, {}); ``` -------------------------------- ### Setting Word Document Properties - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet demonstrates how to set specific metadata properties for Microsoft Word (DOCX) documents, including subject, keywords, and description. These properties can be set during object creation or updated later using dedicated setter methods. ```javascript var docx = officegen({ 'type': 'docx', 'subject': '...', 'keywords': '...', 'description': '...' }); // or docx.setDocSubject('...') docx.setDocKeywords('...') docx.setDescription('...') ``` -------------------------------- ### Creating a Column Chart in PowerPoint (officegen, JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet demonstrates how to create a column chart on a new PowerPoint slide. It configures the chart with a title, render type, axis titles, value axis format, and two data series for 'Income' and 'Expense', each with labels, values, and optional colors. ```javascript // Column chart slide = pptx.makeNewSlide(); slide.name = 'Chart slide'; slide.back = 'ffffff'; slide.addChart( { title: 'Column chart', renderType: 'column', valAxisTitle: 'Costs/Revenues ($)', catAxisTitle: 'Category', valAxisNumFmt: '$0', valAxisMaxValue: 24, data: [ // each item is one serie { name: 'Income', labels: ['2005', '2006', '2007', '2008', '2009'], values: [23.5, 26.2, 30.1, 29.5, 24.6], color: 'ff0000' // optional }, { name: 'Expense', labels: ['2005', '2006', '2007', '2008', '2009'], values: [18.1, 22.8, 23.9, 25.1, 25], color: '00ff00' // optional }] } ) ``` -------------------------------- ### Handling officegen Error Events - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet shows how to register an 'error' event listener on an officegen object. This allows the application to catch and handle any errors that occur during the document generation process, preventing crashes and providing debugging information. ```javascript pptx.on('error', function (err) { console.log(err) }) ``` -------------------------------- ### Styling Tables with Basic Options in officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This snippet defines a `tableStyle` object to customize the appearance and layout of a table in a Word document using `officegen`. It sets properties like column width, font, alignment, spacing, indentation, and border options. The style is then applied when creating the table with `docx.createTable`. ```JavaScript var tableStyle = { tableColWidth: 4261, tableSize: 24, tableColor: "ada", tableAlign: "left", tableFontFamily: "Comic Sans MS", spacingBefor: 120, // default is 100 spacingAfter: 120, // default is 100 spacingLine: 240, // default is 240 spacingLineRule: 'atLeast', // default is atLeast indent: 100, // table indent, default is 0 fixedLayout: true, // default is false borders: true, // default is false. if true, default border size is 4 borderSize: 2, // To use this option, the 'borders' must set as true, default is 4 columns: [{ width: 4261 }, { width: 1 }, { width: 42 }], // Table logical columns } docx.createTable (table, tableStyle); ``` -------------------------------- ### Creating a Bar Chart in PowerPoint (officegen, JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet demonstrates how to create a bar chart on a new PowerPoint slide. It includes multiple data series representing different regions (europe, namerica, asia, lamerica, meast, africa) with their respective labels, values, and optional colors. ```javascript // Bar Chart slide = pptx.makeNewSlide(); slide.name = 'Bar Chart slide'; slide.back = 'ff00ff'; slide.addChart( { title: 'Sample bar chart', renderType: 'bar', data: [ // each item is one serie { name: 'europe', labels: ['Y2003', 'Y2004', 'Y2005'], values: [2.5, 2.6, 2.8], color: 'ff0000' // optional }, { name: 'namerica', labels: ['Y2003', 'Y2004', 'Y2005'], values: [2.5, 2.7, 2.9], color: '00ff00' // optional }, { name: 'asia', labels: ['Y2003', 'Y2004', 'Y2005'], values: [2.1, 2.2, 2.4], color: '0000ff' // optional }, { name: 'lamerica', labels: ['Y2003', 'Y2004', 'Y2005'], values: [0.3, 0.3, 0.3], color: 'ffff00' // optional }, { name: 'meast', labels: ['Y2003', 'Y2004', 'Y2005'], values: [0.2, 0.3, 0.3], color: 'ff00ff' // optional }, { name: 'africa', labels: ['Y2003', 'Y2004', 'Y2005'], values: [0.1, 0.1, 0.1], color: '00ffff' // optional } ] } ) ``` -------------------------------- ### Creating a New Paragraph in officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This snippet demonstrates how to create a new paragraph object using `docx.createP()`. This paragraph object (`pObj`) serves as a container for text, images, and other elements, and is essential for structuring content within the document. ```javascript var pObj = docx.createP (); ``` -------------------------------- ### Setting Document Title Property - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet shows two methods for setting the title of a document (applicable to PPTX, PPSX, DOCX). The title can be set during object instantiation using the 'title' option or dynamically after creation using the `setDocTitle` method. ```javascript var pptx = officegen({ 'type': 'pptx', 'title': '' }); // or pptx.setDocTitle('<title>') ``` -------------------------------- ### Creating Word Document Object - JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/README-basic.md This snippet demonstrates how to create an empty officegen object for generating Microsoft Word (DOCX) documents. This object will be used to add paragraphs, tables, and other Word content. ```javascript var docx = officegen('docx') ``` -------------------------------- ### Creating a Pie Chart in PowerPoint (officegen, JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This snippet shows how to generate a pie chart on a new PowerPoint slide. It defines a single data series for 'Oil' with categories (countries), corresponding values, and an array of colors for each slice. ```javascript // Pie chart slide = pptx.makeNewSlide(); slide.name = 'Pie Chart slide'; slide.back = 'ffff00'; slide.addChart( { title: 'My production', renderType: 'pie', data: [ // each item is one serie { name: 'Oil', labels: ['Czech Republic', 'Ireland', 'Germany', 'Australia', 'Austria', 'UK', 'Belgium'], values: [301, 201, 165, 139, 128, 99, 60], colors: ['ff0000', '00ff00', '0000ff', 'ffff00', 'ff00ff', '00ffff', '000000'] // optional }] } ) ``` -------------------------------- ### Creating a Blank PPTX Slide in JavaScript Source: https://github.com/ziv-barber/officegen/blob/master/manual/pptx/README.md This method creates a new, blank slide object within the PowerPoint presentation. The returned `slide` object can then be used to add various content elements like text, shapes, or images. It is a fundamental step for populating a presentation with content. ```javascript let slide = pptx.makeNewSlide(); ``` -------------------------------- ### Creating Word Document Content with JSON Data in officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This comprehensive snippet illustrates how to define various document elements using JSON structures for `officegen`. It includes a `table` array with cell-specific options, a `tableStyle` object for general table formatting, and a `data` array demonstrating text formatting (color, background, bold, underline), line breaks, horizontal lines, text alignment, font styling, and embedding a table and an image. ```JavaScript var table = [ [{ val: "No.", opts: { cellColWidth: 4261, b:true, sz: '48', shd: { fill: "7F7F7F", themeFill: "text1", "themeFillTint": "80" }, fontFamily: "Avenir Book" } },{ val: "Title1", opts: { b:true, color: "A00000", align: "right", shd: { fill: "92CDDC", themeFill: "text1", "themeFillTint": "80" } } },{ val: "Title2", opts: { align: "center", cellColWidth: 42, b:true, sz: '48', shd: { fill: "92CDDC", themeFill: "text1", "themeFillTint": "80" } } }], [1,'All grown-ups were once children',''], [2,'there is no harm in putting off a piece of work until another day.',''], [3,'But when it is a matter of baobabs, that always means a catastrophe.',''], [4,'watch out for the baobabs!','END'], ] var tableStyle = { tableColWidth: 4261, tableSize: 24, tableColor: "ada", tableAlign: "left", tableFontFamily: "Comic Sans MS" } var data = [[{ type: "text", val: "Simple" }, { type: "text", val: " with color", opt: { color: '000088' } }, { type: "text", val: " and back color.", opt: { color: '00ffff', back: '000088' } }, { type: "linebreak" }, { type: "text", val: "Bold + underline", opt: { bold: true, underline: true } }], { type: "horizontalline" }, [{ backline: 'EDEDED' }, { type: "text", val: " backline text1.", opt: { bold: true } }, { type: "text", val: " backline text2.", opt: { color: '000088' } }], { type: "text", val: "Left this text.", lopt: { align: 'left' } }, { type: "text", val: "Center this text.", lopt: { align: 'center' } }, { type: "text", val: "Right this text.", lopt: { align: 'right' } }, { type: "text", val: "Fonts face only.", opt: { font_face: 'Arial' } }, { type: "text", val: "Fonts face and size.", opt: { font_face: 'Arial', font_size: 40 } }, { type: "table", val: table, opt: tableStyle }, [{ // arr[0] is common option. align: 'right' }, { type: "image", ``` -------------------------------- ### Setting Paragraph Options in officegen (JavaScript) Source: https://github.com/ziv-barber/officegen/blob/master/manual/docx/README.md This code illustrates how to configure various options for a paragraph object (`pObj`) after its creation. Options include text alignment ('center', 'right', 'justify') and indentation settings (`indentLeft`, `indentFirstLine`), allowing for precise layout control. ```javascript pObj.options.align = 'center'; // Also 'right' or 'justify'. pObj.options.indentLeft = 1440; // Indent left 1 inch pObj.options.indentFirstLine = 440; // Indent first line ```