function main(page: IPage, portlet: ITableSheetPortlet) {
let r = portlet.getRowCount(); //获取电子表格总行数
for (let i = 2; i < r - 1; i++) {
// 设置类别
let cell = portlet.getCell(i, 1); //根据行列号获取单元格
truncateCellInnerText(cell, 5); //设置显示的字符个数
// 设置名称
cell = portlet.getCell(i, 2);
truncateCellInnerText(cell, 2);
//设置数量
cell = portlet.getCell(i, 3);
truncateCellInnerText(cell, 4);
}
}
// 如果单元格内容超出指定长度、将其截短
function truncateCellInnerText(cell: ISheetCell, textMaxLength: number) {
if (!cell) {
return;
}
let len = textMaxLength || 10;
let txt = cell.getText();
if (txt.length > len) {
cell.setText(txt.substring(0, len) + "...");
cell.setAttribute("title", txt);
}
}
function strlen(str: string) {
let len = 0;
for (let i = 0; i < str.length; i++) {
let c = str.charCodeAt(i);
//单字节加1
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
len++;
} else {
len += 2;
}
}
return len;
}