100金币
附件中有ssjson,目前的场景是有三个sheet,原材料附表,水泥混凝土配合比设计,报告,每个sheet都有一个table;
初始化时,调用后端接口,拿到数据后,给三个sheet页中的table设置数据,在原材料附表中没有问题;
在水泥混凝土配合比设计,报告这两个报告中,绑定了数据,发现有些字段赋值不成功:
同时,在table下面的行,行高发生了变化:
以下是我的代码部分
- processQualification() {
- let arr = this.form.sampleInfoMaterialRelationDtoList;
- if (arr && arr.length) {
- arr.forEach((item, index) => {
- item.index = index + 1;
- if (item.qualificationName) {
- // 使用JSON.parse将字符串转换为对象
- const qualificationObject = JSON.parse(item.qualificationName);
- // 使用Object.keys获取对象的所有可枚举属性名
- const keys = Object.keys(qualificationObject);
- // 创建一个空数组来存储键值对字符串
- let keyValuePairs = keys.map(
- (key) => `${key}:${qualificationObject[key]}`
- );
- item.qualificationName = keyValuePairs.join(";");
- }
- });
- }
- return arr;
- },
- processMaterialSheet(sheet, spreadNS, arr, data) {
- let table = sheet.tables.findByName("Table1");
- if (!table) {
- console.error("未找到名为 'Table1' 的表格");
- return;
- }
- let totalRowCount = sheet.getRowCount(); // 获取当前工作表的总行数
- let rowCountToAdd = arr.length - 1; // 要添加的行数
- // 尝试获取basicrow名称管理器的范围
- let customName = sheet.getCustomName("basicrow");
- // 检查basicrow是否存在
- if (!customName) {
- console.error("命名范围 'basicrow' 未找到。");
- return;
- }
- // 获取basicrow的表达式
- let expression = customName.getExpression();
- // 获取basicrow的表达式
- if (!expression) {
- console.error("命名范围 'basicrow' 的表达式未定义。");
- return;
- }
- // 获取basicrow的范围
- let basicrow = expression.getRange();
- // 获取basicrow的范围
- if (!basicrow) {
- console.error("无法获取 'basicrow' 的范围。");
- return;
- }
- let startRow = basicrow.row; // basicrow的起始行
- let rowHeight = sheet.getRowHeight(startRow); // 获取basicrow行高
- let tableRange = table.range(); // 获取表格范围
- let currentRowCount = tableRange.rowCount; // 当前表格行数
- let row = tableRange.row; // 表格起始行
- // 在工作表末尾添加行
- sheet.addRows(totalRowCount, rowCountToAdd * basicrow.rowCount);
- // 在表格中插入新行
- table.insertRows(
- currentRowCount - 2,
- rowCountToAdd * basicrow.rowCount,
- true
- );
- // 设置新行的行高
- for (let i = 0; i <= rowCountToAdd * basicrow.rowCount + 3; i++) {
- console.log(currentRowCount + row + i, rowHeight);
- sheet.setRowHeight(currentRowCount + row + i, rowHeight);
- }
- for (let i = 0; i < rowCountToAdd; i++) {
- sheet.copyTo(
- startRow,
- basicrow.col,
- currentRowCount + row + basicrow.rowCount * i,
- basicrow.col,
- basicrow.rowCount,
- basicrow.colCount,
- GC.Spread.Sheets.CopyToOptions.style |
- GC.Spread.Sheets.CopyToOptions.formula |
- GC.Spread.Sheets.CopyToOptions.span
- );
- }
- // table.expandBoundRows(true);
- // 创建表格列对象
- const columnNames = [
- "index",
- "experienceItemName",
- "sampleCode",
- "experienceItemParamNames",
- "qualificationName",
- "sampleNum",
- "manufacturer",
- "origin",
- "batchNumber",
- "sampleDesc",
- "recommendDosage",
- ];
- let tableColumns = columnNames.map(
- (columnName) => new spreadNS.Tables.TableColumn(columnName, columnName)
- );
- // 设置表格不自动生成列
- table.autoGenerateColumns(false);
- // 绑定数据
- table.bind(tableColumns, "sales", data);
- },
- processReportSheet(sheet, spreadNS, arr, data) {
- let table = sheet.tables.findByName("reportRecordProportion");
- if (!table) {
- console.error("未找到名为 'reportRecordProportion' 的表格");
- return;
- }
- let totalRowCount = sheet.getRowCount(); // 获取当前工作表的总行数
- let rowCountToAdd = arr.length - 1; // 要添加的行数
- // 尝试获取basicrow名称管理器的范围
- let customName = sheet.getCustomName("basicrow");
- // 检查basicrow是否存在
- if (!customName) {
- console.error("命名范围 'basicrow' 未找到。");
- return;
- }
- // 获取basicrow的表达式
- let expression = customName.getExpression();
- // 获取basicrow的表达式
- if (!expression) {
- console.error("命名范围 'basicrow' 的表达式未定义。");
- return;
- }
- // 获取basicrow的范围
- let basicrow = expression.getRange();
- // 获取basicrow的范围
- if (!basicrow) {
- console.error("无法获取 'basicrow' 的范围。");
- return;
- }
- let startRow = basicrow.row; // basicrow的起始行
- let rowHeight = sheet.getRowHeight(startRow); // 获取basicrow行高
- let tableRange = table.range(); // 获取表格范围
- let currentRowCount = tableRange.rowCount; // 当前表格行数
- let row = tableRange.row; // 表格起始行
- // 在工作表末尾添加行
- sheet.addRows(totalRowCount, rowCountToAdd * basicrow.rowCount);
- // 在表格中插入新行
- table.insertRows(
- currentRowCount - 2,
- rowCountToAdd * basicrow.rowCount,
- true
- );
- // 设置新行的行高
- for (let i = 0; i <= rowCountToAdd * basicrow.rowCount + 3; i++) {
- console.log(currentRowCount + row + i, rowHeight);
- sheet.setRowHeight(currentRowCount + row + i, rowHeight);
- }
- for (let i = 0; i < rowCountToAdd; i++) {
- sheet.copyTo(
- startRow,
- basicrow.col,
- currentRowCount + row + basicrow.rowCount * i,
- basicrow.col,
- basicrow.rowCount,
- basicrow.colCount,
- GC.Spread.Sheets.CopyToOptions.style |
- GC.Spread.Sheets.CopyToOptions.formula |
- GC.Spread.Sheets.CopyToOptions.span
- );
- }
- // table.expandBoundRows(true);
- // 创建表格列对象
- const columnNames = [
- "experienceItemName",
- "qualificationName",
- "manufacturer",
- "sampleCode",
- ];
- let tableColumns = columnNames.map(
- (columnName) => new spreadNS.Tables.TableColumn(columnName, columnName)
- );
- // 设置表格不自动生成列
- table.autoGenerateColumns(false);
- // 绑定数据
- table.bind(tableColumns, "sales", data);
- },
- processOriginalSheet(sheet, spreadNS, arr, data) {
- let table = sheet.tables.findByName("originalRecordProportion");
- if (!table) {
- console.error("未找到名为 'originalRecordProportion' 的表格");
- return;
- }
- let totalRowCount = sheet.getRowCount(); // 获取当前工作表的总行数
- let rowCountToAdd = arr.length - 1; // 要添加的行数
- // 尝试获取basicrow名称管理器的范围
- let customName = sheet.getCustomName("basicrow");
- // 检查basicrow是否存在
- if (!customName) {
- console.error("命名范围 'basicrow' 未找到。");
- return;
- }
- // 获取basicrow的表达式
- let expression = customName.getExpression();
- // 获取basicrow的表达式
- if (!expression) {
- console.error("命名范围 'basicrow' 的表达式未定义。");
- return;
- }
- // 获取basicrow的范围
- let basicrow = expression.getRange();
- // 获取basicrow的范围
- if (!basicrow) {
- console.error("无法获取 'basicrow' 的范围。");
- return;
- }
- let startRow = basicrow.row; // basicrow的起始行
- let rowHeight = sheet.getRowHeight(startRow); // 获取basicrow行高
- let tableRange = table.range(); // 获取表格范围
- let currentRowCount = tableRange.rowCount; // 当前表格行数
- let row = tableRange.row; // 表格起始行
- // 在工作表末尾添加行
- sheet.addRows(totalRowCount, rowCountToAdd * basicrow.rowCount);
- // 在表格中插入新行
- table.insertRows(
- currentRowCount - 2,
- rowCountToAdd * basicrow.rowCount,
- true
- );
- // 设置新行的行高
- for (let i = 0; i <= rowCountToAdd * basicrow.rowCount + 3; i++) {
- console.log(currentRowCount + row + i, rowHeight);
- sheet.setRowHeight(currentRowCount + row + i, rowHeight);
- }
- for (let i = 0; i < rowCountToAdd; i++) {
- sheet.copyTo(
- startRow,
- basicrow.col,
- currentRowCount + row + basicrow.rowCount * i,
- basicrow.col,
- basicrow.rowCount,
- basicrow.colCount,
- GC.Spread.Sheets.CopyToOptions.style |
- GC.Spread.Sheets.CopyToOptions.formula |
- GC.Spread.Sheets.CopyToOptions.span
- );
- }
- // table.expandBoundRows(true);
- // 创建表格列对象
- const columnNames = [
- "experienceItemName",
- "qualificationName",
- "manufacturer",
- "sampleCode",
- ];
- let tableColumns = columnNames.map(
- (columnName) => new spreadNS.Tables.TableColumn(columnName, columnName)
- );
- // 设置表格不自动生成列
- table.autoGenerateColumns(false);
- // 绑定数据
- table.bind(tableColumns, "sales", data);
- },
- processingMaterialInfo(spread) {
- let spreadNS = GC.Spread.Sheets;
- const sheetCount = spread.getSheetCount();
- let arr = this.processQualification();
- let data = {
- name: "Jones",
- region: "East",
- sales: arr,
- };
- for (let i = 0; i < sheetCount; i++) {
- const sheet = spread.getSheet(i);
- let sheetName = sheet.name();
- if (sheetName === "原材料附表") {
- this.processMaterialSheet(sheet, spreadNS, arr, data);
- } else if (sheetName === "报告") {
- this.processReportSheet(sheet, spreadNS, arr, data);
- } else if (sheetName.includes("配合比")) {
- this.processOriginalSheet(sheet, spreadNS, arr, data);
- }
- console.log(sheetName);
- }
- },
复制代码
|
最佳答案
查看完整内容
您好,
第一个问题:为什么【原材料附表】可以数据展示完整,而【水泥混凝土配合比设计】和【报告】数据展示不完整?
因为【原材料附表】没有进行单元格合并,一列对应一个字段。
而【水泥混凝土配合比设计】和【报告】均有单元格合并,以【水泥混凝土配合比设计】为例,为前四列赋值Column字段,
所以A列显示experienceItemName字段,而D列(第二列标题)显示的是第四个字段:sampleCode。如下图所示
所以正确的绑定字段 ...
|