Posted 15 October 2024, 6:30 am EST - Updated 15 October 2024, 6:35 am EST
Hello,
Althogth I wanted a right method to get a table column definition (or its definition id) from a column index, it seems to be unanable as for now. So, as an alternative, I use the table.getColumnName method to determine whether the current table column is a specific column. Please see the codes below.
The problem is, when the column header name was changed by pasting some value or pushing ‘Delete’ key on the table column header, my approach doesn’t work well. Could you tell me if you have any workaround?
import * as GC from '@mescius/spread-sheets';
import '@mescius/spread-sheets/styles/gc.spread.sheets.excel2016black.css';
import * as React from 'react';
import './App.css';
import Sheets = GC.Spread.Sheets;
const columns: Sheets.Tables.TableColumn[] = [
new Sheets.Tables.TableColumn(0, "name", "Name"),
new Sheets.Tables.TableColumn(1, "address", "Address"),
new Sheets.Tables.TableColumn(2, "phone", "Phone"),
new Sheets.Tables.TableColumn(3, "email", "Email"),
];
export default function App() {
const host = React.useRef<HTMLDivElement>();
React.useEffect(
() => {
const workbook = new Sheets.Workbook(host.current, {sheetCount: 1});
const sheet = workbook.getSheet(0);
const table = sheet.tables.add("table1", 1, 1, 4, 4, Sheets.Tables.TableThemes.light1);
table.bindColumns(columns);
// Prohibit from editing a value on the 'name' column...
sheet.bind(Sheets.Events.EditStarting, (_: unknown, args: { row: number, col: number, cancel: boolean }) => {
if (Object.is(table, sheet.tables.find(args.row, args.col))) {
const tableColumnIndex = args.col - table.range().col;
const name = table.getColumnName(tableColumnIndex);
if (name === columns[0].name()) {
args.cancel = true;
}
}
});
return () => workbook.destroy();
},
[]
);
return (
<div ref={host} style={{ height: "90vh", width: "100%" }} />
);
}