aardio-读写Excel

2020年08月05日 43点热度 0人点赞 0条评论
向Excel文件写入内容实例

import win.ui;
import win.ui.atom;
import com;
import wps.et
/*DSG{{*/
var winform = win.form(text="续航Log自动录入Excel工具";right=478;bottom=522;acceptfiles=1;bgcolor=16777215;parent=...)
winform.add(
combobox={cls="combobox";left=188;top=469;right=250;bottom=489;edge=1;items={"显示";"不显示"};mode="dropdownlist";z=7};
edit={cls="edit";left=40;top=47;right=436;bottom=78;acceptfiles=1;border=1;edge=1;font=LOGFONT(h=-14;name='微软雅黑';weight=700);multiline=1;z=3};
groupbox={cls="groupbox";text="将Log文件拖放到此处";left=28;top=149;right=446;bottom=447;edge=1;z=2};
groupbox2={cls="groupbox";text="模拟续航测试用例.xlsx文件路径";left=29;top=21;right=446;bottom=136;edge=1;z=1};
input={cls="button";text="录入";left=322;top=463;right=441;bottom=504;font=LOGFONT(h=-21;name='微软雅黑';weight=700);z=4};
listbox={cls="listbox";left=38;top=170;right=436;bottom=439;acceptfiles=1;edge=1;frame=1;items={};vscroll=1;z=5};
open={cls="button";text="打开“模拟续航测试用例”excel文件";left=38;top=84;right=436;bottom=129;z=6};
static={cls="static";text="是否显示打开的Excel文件";left=48;top=474;right=204;bottom=495;transparent=1;z=8}
)
/*}}*/

var FileFullPath;
var FileFullPathLength;

winform.wndproc = function(hwnd,message,wparam,lparam){
    select(message) {//判断消息类型
        case 0x233/*_WM_DROPFILES*/{
            FileFullPath=win.getDropFile(wparam)[1]
            FileFullPathLength=#FileFullPath
    		winform.listbox.clear();//清除listbox内容
            for line in io.lines(FileFullPath){
            	new_line = string.replace(line," ","");
            	if(string.endWith(new_line,"mv") or 
            	   string.endWith(new_line,"%") or 
            	   string.endWith(new_line,"min")){
            		winform.listbox.add(new_line,-1);
            	}
            }
        }
    }
    //无返回值则继续调用默认回调函数
}
winform.combobox.selText = "显示"

import fsys.dlg

var ExcelFilePath;

winform.open.oncommand = function(id,event){
    winform.edit.text = fsys.dlg.open("Excel文件(*.xlsx;*.xls)|*.xlsx;*.xls|所有文件(*.*)|*.*|")
	ExcelFilePath = winform.edit.text;
    if(#ExcelFilePath){
		
		//录入按钮 开始
		winform.input.oncommand = function(id,event){
			try {
				excel = wps.et()
			}
			if(!excel) error("未检测到excel",2)
			
			var book  = excel.Open(ExcelFilePath);
			var sheet = book.Worksheets("实际操作模型(非低电)")
			if(winform.combobox.text == "显示"){excel.Visible = true;}
			if(winform.combobox.text == "不显示"){excel.Visible = false;}
			//sheet.Range("J"+5).value2="写入excel";
		
    		if(#FileFullPath){
    			/*
    				未对路径做判断
    				未对文件里内容做非空判断
    			*/
    			mv_i=5;
    			pw_i=5;
				for line in io.lines(FileFullPath){
					new_line = string.replace(line," ","");  //Remove spaces from strings.
					if(#line){
						if(string.endWith(new_line,"mv")){
							str_mv = string.slice(new_line,-6,-3);
							if(mv_i = 5){
								//第一次电压为开始电压,记录在表格 K5 上
								sheet.Range("K"+mv_i).value2=str_mv;
							}
							else{
								sheet.Range("L"+mv_i-1).value2=str_mv;
							}
							io.print(new_line);
							mv_i++;
						}
						if(string.endWith(new_line,"%")){
							if(pw_i = 5){
								str_pw = string.slice(new_line,-4,-2);
								if(string.left(str_pw,1) != "=" and string.left(str_pw,1) != "s"){
									sheet.Range("I"+pw_i).value2="1";
								}
								else{
									new_str_pw = string.slice(new_line,-3,-2);
									if(string.left(new_str_pw,1) != "=" and string.left(str_pw,1) != "s"){
										new_pw = string.concat("0.",new_str_pw);
										//winform.msgbox(pw_i)
										sheet.Range("I"+pw_i).value2=new_pw;
									}
									else{
										new_pw = string.slice(new_line,-2,-2);
										new_pw = string.concat("0.0",new_pw);
										sheet.Range("I"+pw_i).value2=new_pw;
									}
								}	
							}
							else{
								str_pw = string.slice(new_line,-4,-2);
								if(string.left(str_pw,1) != "=" and string.left(str_pw,1) != "s"){
									sheet.Range("J"+pw_i-1).value2="1";
								}
								else{
									new_str_pw = string.slice(new_line,-3,-2);
									if(string.left(new_str_pw,1) != "=" and string.left(str_pw,1) != "s"){
										new_pw = string.concat("0.",new_str_pw);
										//winform.msgbox(pw_i)
										sheet.Range("J"+pw_i-1).value2=new_pw;
									}
									else{
										new_pw = string.slice(new_line,-2,-2);
										new_pw = string.concat("0.0",new_pw);
										sheet.Range("J"+pw_i-1).value2=new_pw;
									}
								}
							}
							pw_i++;
						}
						if(string.endWith(new_line,"min")){
							//时长:场景时长--表格 H5 为起始点
						}
					}
					else{
						//string 为空
					}
				}
				winform.msgbox("续航数据录入成功!")
    		}
    		else{
    			winform.msgbox("未选择 续航自动化Log 文件!")
    		}
		}
		//录入按钮 结束
	}
	else{
		winform.msgbox("未打开“模拟续航测试用例Excle”文件!")
	}
}




winform.show() 
win.loopMessage();

Hang.Zhang

“舍”是一种觉悟,更是一种自由

文章评论