tools/txt2table.aardio

473 lines
11 KiB
Plaintext
Raw Normal View History

2023-07-23 22:23:24 +08:00
import fonts.fontAwesome;
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=815;bottom=567)
winform.add(
btnWbPrint={cls="button";text="Print";left=752;top=448;right=792;bottom=472;z=9};
button={cls="button";text="Button";left=744;top=520;right=800;bottom=552;z=3};
button2={cls="button";text="Copy";left=736;top=104;right=784;bottom=128;z=4};
combobox={cls="combobox";left=736;top=168;right=808;bottom=192;edge=1;items={};mode="dropdownlist";z=8};
edit={cls="edit";left=16;top=16;right=728;bottom=280;db=0.51;dl=1;dr=0.11;dt=1;edge=1;font=LOGFONT(name='Consolas');hscroll=1;multiline=1;vscroll=1;z=1};
editOri={cls="edit";text="Edit";left=16;top=288;right=728;bottom=552;db=1;dl=1;dr=0.11;dt=0.51;edge=1;font=LOGFONT(name='Consolas');hscroll=1;multiline=1;vscroll=1;z=2};
plusCenter={cls="plus";left=760;top=64;right=780;bottom=84;bgcolor=-5197169;font=LOGFONT(h=-13);iconStyle={font=LOGFONT(h=-13;name='FontAwesome')};iconText='\uF037';textPadding={left=20};z=7};
plusLeft={cls="plus";left=736;top=64;right=756;bottom=84;bgcolor=-5197169;font=LOGFONT(h=-13);iconStyle={font=LOGFONT(h=-13;name='FontAwesome')};iconText='\uF036';notify=1;textPadding={left=20};z=5};
plusRight={cls="plus";left=784;top=64;right=804;bottom=84;bgcolor=-5197169;font=LOGFONT(h=-13);iconStyle={font=LOGFONT(h=-13;name='FontAwesome')};iconText='\uF038';textPadding={left=20};z=6}
)
/*}}*/
/***
+完成每列等宽设计
+完成行内换行设计
++完成换行后行内垂直居中设计
+完成对齐功能L、C、R
+完成列合并功能
++完成列合并后居中功能
+完成行合并功能
--待完成行合并后垂直居中功能
*修复列宽bug
2024-06-08 20:49:33 +08:00
+添加\\r转义
*修复\-结尾的bug
2023-07-23 22:23:24 +08:00
***/
import console
winform.editOri.text = /***
2024-06-08 20:49:33 +08:00
列一 列二 列三 列四 列五
"\|"表示下行与前行合并 "\-"表示后列与前列合并 \- \- SRSlot
\| "\\r"\r表\r示\r换\r行 自举子帧 BootSFrm NEW
\| 自举应答子帧 RBootSFrm 同步时隙 \|
参数分发子帧 PSFrm \- \-
2023-07-23 22:23:24 +08:00
***/
/***
"\r"代表单元格内换行
"\-"代表与前列合并单元格,其后值忽略
"\|"代表与前行合并单元格
***/
winform.combobox.items = {"空白字符";"制表符";"空格"}
winform.combobox.selIndex =1
2024-06-08 20:49:33 +08:00
align = "Center"
2023-07-23 22:23:24 +08:00
str2xls = function(str,ss,s,c){
if(!s){
s= "-"
}
if(!c){
c="+"
}
if(!ss){
ss=s
}
var strXls = "";
string.appendText = function(str){
strXls = strXls ++ str
}
var delta = 1; //默认两侧各多1个空格
var Rowstr = string.splitEx(str,'\r\n')//分行
var pattern = {"\s+";"\t+";" +"}; //截断规则
var Rowarr = table.map(Rowstr,lambda(v,k) string.splitEx(string.trimright(v),pattern[winform.combobox.selIndex]) )//分列
var Cmax = table.map(Rowarr,lambda(v,k) #v );
table.sort(Cmax)//获取最大列数Cmax[#Cmax]
var Rmax={}//最大子行数
RowarrRN = table.map(Rowarr,function(v,k,result){//分子行
var a = {};
var rr = 1;
//console.dump(v)
for(i=1;Cmax[#Cmax];1){//#tmp[1]获取最大列而非#v是避免遗漏空列
2024-06-08 20:49:33 +08:00
var x = string.splitEx(v[i]==null?" ":v[i],"!\\\\r"); //||代表元素换行
x = table.map(x,lambda(v,k) string.replace(v,"@\\r","\r") )//去掉\\转义
2023-07-23 22:23:24 +08:00
table.push(a,x)
if(rr<#x){//当前行内排序,找出最大的子行数
rr=#x
}
}
table.push(Rmax,rr)
return a;
});
//console.dump(RowarrRN)
vc = function(arg,max){//数组元素居中
var arr = arg;
var num = math.floor((max-#arr)/2);
while(num){
table.insert(arr,"")
num = num-1
}
arr = table.slice(table.mix(arr,table.array(max,"")),1,max)
return arr;
}
vc1line = function(arg){//行内(换行)垂直居中
var tmp = arg;
for(i=1;#tmp;1){
for(j=1;#tmp[1];1){
tmp[i][j] = vc(tmp[i][j],Rmax[i])
}
}
return tmp;
}
var tmp = vc1line(RowarrRN)
vc2line = function(i,j){//连续行(合并行)垂直居中
arr = vc(RowarrRN[i-1][j],Rmax[i-1]+2)
return arr;
}
//console.dump(vc2line(3,3))
glA = function(str){//获取字符中ASCII字符个数
var llA = {};
string.search(llA,str,"\i" )
return #llA;
}
gw = function(){ //获取每列最大列宽
var wmax = {}; //每列最大列宽
for(j=1;#tmp[1];1){//col
var llA = glA(tmp[1][j][1])
var w = string.len(tmp[1][j][1])-math.floor(llA/2)//;#tmp[1][j][1];#l1A}; //字符宽字节宽ASCII个数
for(i=1;#tmp;1){//row
for(ii=1;Rmax[i];1){
if(tmp[i][j][ii]==null){
tmp[i][j][ii]= ""
}
var llA = glA(tmp[i][j][ii])
var l = string.len(tmp[i][j][ii])-math.floor(llA/2)
if(w <= l){//字符最长
w = l//字符宽
}
}
}
table.push(wmax,w)
}
return wmax;
}
var UserWmax = table.clone(gw())
//console.dump(UserWmax)
txt2arrOri = function(){
var newstr = {};
for(i=1;#tmp;1){//row
for(ii=1;Rmax[i];1){//元素换行
var rowsep={}
var rowtxt={}
for(j=1;#tmp[1];1){
//
//新数组,[行号]={连接符; 单元格字符; sta;对应原txt所在行}
var con = c;
var dstr = string.repeat((UserWmax[j]+delta)*2,s)
//var l = (UserWmax[j]-string.len(tmp[i][j][ii]))*2+math.round(glA(tmp[i][j][ii]))+2*delta; //需添加空格数
if(i == 2){//表头,last
//dstr = string.repeat((UserWmax[j]+delta)*2,ss);
}
if(ii == 1){//子行不添加分割行
table.push(rowsep,{con;dstr;0})
}
table.push(rowtxt,{"|";tmp[i][j][ii];0;i})
}//end j
if(rowsep[1]!=null){
table.push(newstr,rowsep)//new row in cell;
}
table.push(newstr,rowtxt)
//console.dump(newstr[i][2])
}//end ii
}//end i
return newstr;
}
//var txtArr = txt2arrOri()
//console.dump(txt2arrOri())
formatarr = function(a){
var arg = a;
gs = function(){
var a = table.array(#arg,#arg[1],0);
for(i=1;#arg;1){//row
for(j=1;#arg[1];1){
select(arg[i][j][2]) {
case "\-" {//优先处理行内合并
a[i][j]=-1
if(j>1){
a[i][j-1]=-1
}
}
case "\|" {
a[i][j]=1
}
else {
a[i][j]=0
}
}
}
}
return a;
}
var sta = gs()
//console.dump(sta)
//align
aCell = function(str,w){//align current cell
if(w==null){
//w=UserWmax[j]
}
var ll = (w-string.len(str))*2+math.round(glA(str))+2*delta; //需添加空格数
select(align) {
case "Center" {//center
return string.repeat(math.floor(ll/2)," ") ++ str ++ string.repeat(ll-math.floor(ll/2)," "))
}
case "Right" {//right
return string.repeat(ll-delta," ") ++ str ++ string.repeat(delta," "))
}
else {//left
return string.repeat(delta," ") ++ str ++ string.repeat(ll-delta," "))
}
}
}
var tmpalign = align
for(i=1;#arg;1){//row
for(j=1;#arg[1];1){
if(arg[i][j][1] != c){//only txt
if(i == 2){//表头强制居中
align = "Center"
}
else {
align = tmpalign
}
arg[i][j][2] = aCell(arg[i][j][2],UserWmax[j])
}
}
}
var Rc = Rowarr;
var xd = txt2arrOri();
for(i=1;#arg;1){//row
for(j=1;#arg[1];1){
if(arg[i][j][1] != c){//only txt
//行合并
if(xd[i][j][2] == "\|" and i != 1 and sta[i-1] != -1){
//xd[i][j][2] = ""
var k = 1;
var r2c = Rmax[arg[i][j][4]] + Rmax[arg[i][j][4]-1]+1;
var ori = {};
var r1 = i-math.floor((Rmax[arg[i][j][4]]-1)/2)-Rmax[arg[i][j][4]-1]-1
for(r=r1;r1+Rmax[arg[i][j][4]-1]-1;1){
table.push(ori,xd[r][j][2])
}
//while(xd[i+2*(k)][j][2] == "\|"){
while(Rc[arg[i][j][4]+k][j] == "\|"){//txt下一行同列
//console.dump(k,Rmax[arg[i][j][4]+k])
Rc[arg[i][j][4]+k][j] = ""
math.floor((Rmax[arg[i][j][4]]-1)/2)
r2c += Rmax[arg[i][j][4]+k]+1
if(k!=#arg){
k += 1
}
//console.dump(Rc)
}
ori = vc(ori,r2c)
//console.dump(ori)
for(r=1;#ori;1){
xd[r1+r-1][j][2]=""
arg[r1+r-1][j][2]=aCell(ori[r],UserWmax[j])
}
}
//行内合并
if(xd[i][j][2] == "\-" and j != 1){//第一列无效
xd[i][j][2] = ""
//check how many cells to compose
var k = 1;
var w = UserWmax[j]+UserWmax[j-1];
2024-06-08 20:49:33 +08:00
//未能处理最后一列全为\-的情况暂用try跳过
try{
while(xd[i][j+k][2] == "\-"){
//console.log(k)
xd[i][j+k][2] = ""
w += UserWmax[j+k]
if(k!=#arg[1]){
k += 1
}
2023-07-23 22:23:24 +08:00
}
}
//force align to center
var r2c = Rmax[arg[i][j-1][4]];
for(r=i-math.floor((r2c-1)/2);i+r2c-math.floor((r2c-1)/2)-1;1){
var str = txt2arrOri()[r][j-1][2];
var ll = (w-string.len(str))*2+math.round(glA(str))+2*delta+(2*delta+1)*k; //需添加空格数
arg[r][j-1][2] = string.repeat(math.floor(ll/2)," ") ++ str ++ string.repeat(ll-math.floor(ll/2)," ")
//arg[r][j][2] = ""
for(n=0;k-1;1){
arg[r][j+n][2] = ""
}
}
}//end
}
}
}
return arg;
}
arr2str = function(arr){
var strs = table.map(arr,function(v,k,result){
var c = v[1][1];
//var str = table.map(v,lambda(vv,kk) vv[2])
var str = {}
for(i=1;#v;1){//去掉空cell数组大小会减小map不改变数组大小
if(v[i][2]!=""){
table.push(str,v[i][2])
}
}
return c++string.join(str,c)++c
})
//console.dump(strs)
strXls = string.join(strs,'\r\n') ++ '\r\n' ++ strs[1]
}
arr2str(formatarr(txt2arrOri()))
return "<pre>" ++ strXls ++"</pre>";
}//end str2xls
//write-space:pre|pre-line;
import web.form
var wb = web.form(winform.edit);
wb.html = /**
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
</script>
<style type="text/css">
pre,body {
font-family: 仿宋;
font-size:12px;
color: black;
background-color: #f8f8f8;
display: block;
line-height:1;
}
h1{
font-size:inherit;
font-weight: bold;
color: #4183C4;
write-space:pre-line;
}
</style>
</head>
<body>
</body>
</html>
**/
winform.button2.oncommand = function(id,event){
wb.execWb( 0x11/*_OLECMDID_SELECTALL*/,0 )
wb.execWb( 0xC/*_OLECMDID_COPY*/,0 )
}
winform.button.oncommand = function(id,event){
wb.body.innerHTML = str2xls(winform.editOri.text,"=");
}
for(name,ctrl in winform.eachControl("plus","plus*") ){
ctrl.skin({
background={
default=0x668FB2B0;
disabled=0xFFCCCCCC;
hover=0xFF928BB3
};
color={
default=0xFF000000;
disabled=0xFF6D6D6D
}
})
ctrl.oncommand = function(id,event){
align = string.trimleft(name,"plus");
winform.button.oncommand()
}
}
winform.button.oncommand()
winform.editOri.onChange = function(){
winform.button.oncommand()
}
winform.combobox.oncommand= function(id,event){
var text = owner.selText;
if(event == 9/*_CBN_SELENDOK*/){
winform.button.oncommand()
}
}
//HTML格式打印
winform.btnWbPrint.oncommand = function(id,event){
wb.getDoc().execCommand("print")
}
winform.show();
2023-07-23 22:34:26 +08:00
win.loopMessage();
// todo fix bugs