Background
In a bound Wijmo FlexSheet, the first sheet row displays the data source’s column names. To display a report date above those names, wait until FlexSheet has created the bound header, then insert a styled row at the top of the sheet.
Steps to Complete
- Install the Wijmo packages.
- Insert a row above the bound header.
- Populate the new row.
- Render FlexSheet and handle
loadedRows.
Getting Started
Install the Wijmo packages
npm install @mescius/wijmo.grid @mescius/wijmo.grid.sheet @mescius/wijmo.react.grid.sheet @mescius/wijmo.styles
- mescius/wijmo.grid provides the
Rowclass used to create the new date row. - mescius/wijmo.grid.sheet provides the APIs for binding data and writing cell values, plus the
HeaderRowclass used to identify the bound data header. - mescius/wijmo.react.grid.sheet provides the
<FlexSheet>component that renders the spreadsheet in React. - mescius/wijmo.styles provides the stylesheet that gives the inserted row its header styling through the
wj-headerclass.
Insert a row above the bound header
import { useEffect, useRef } from "react";
import { Row } from "@mescius/wijmo.grid";
import { HeaderRow } from "@mescius/wijmo.grid.sheet";
import { FlexSheet } from "@mescius/wijmo.react.grid.sheet";
import "@mescius/wijmo.styles/wijmo.css";
function insertDateRow(sheet) {
if (!(sheet.rows[0] instanceof HeaderRow)) {
return;
}
const row = new Row();
row.cssClass = "wj-header";
row.align = "left";
sheet.rows.splice(0, 0, row);
writeDate(sheet);
}
- Add the Wijmo imports and helper.
- The helper checks that the first row is the bound
HeaderRowbefore inserting a new row at index0. That check also prevents duplicate date rows ifloadedRowsfires again while the date row is already present. Thewj-headerclass applies the Wijmo theme’s header styling.
Populate the new row
function writeDate(sheet) {
sheet.setCellData(0, 0, "Date:", false);
sheet.setCellData(0, 1, new Date(), false);
}
- The two
setCellDatacalls storeDate:in A1 and the current date in B1. Passingfalseas the fourth argument preserves the supplied values without converting them to the bound columns’ data types.
Render FlexSheet and handle loadedRows
const reportData = [
{ country: "US", product: "Widget", amount: 125 },
{ country: "Germany", product: "Gadget", amount: 240 },
];
function initializeSheet(sheet) {
sheet.addBoundSheet("Country", reportData);
}
export default function ReportSheet() {
const timer = useRef(null);
useEffect(() => {
return () => clearTimeout(timer.current);
}, []);
function handleLoadedRows(sheet) {
clearTimeout(timer.current);
timer.current = setTimeout(() => insertDateRow(sheet), 10);
}
return (
<FlexSheet
initialized={initializeSheet}
loadedRows={handleLoadedRows}
style={{ height: 320 }}
/>
);
}
- The
loadedRowsprop connects the React component to Wijmo’s row-loading event. The handler defers insertion so FlexSheet can finish placing the bound header. Theinitializedcallback binds the sample data, and the effect clears any pending timer when the component unmounts.
With this setup, the React FlexSheet waits for the bound rows to load, inserts a styled row above the data header, and writes the current date into that row.
Happy coding!
Andrew Peterson
Technical Engagement Engineer
