From c0a6c25c5fb6564de6beadc317326fc233c31728 Mon Sep 17 00:00:00 2001 From: xiyoo0812 Date: Tue, 23 Apr 2024 14:38:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E5=AF=BC=E8=A1=A8=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/excel2lua/convertor.lua | 94 +++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/tools/excel2lua/convertor.lua b/tools/excel2lua/convertor.lua index 78af79c3..39678078 100644 --- a/tools/excel2lua/convertor.lua +++ b/tools/excel2lua/convertor.lua @@ -6,6 +6,7 @@ local lexcel = require("luaxlsx") local pairs = pairs local iopen = io.open local ldir = lstdfs.dir +local lstem = lstdfs.stem local lmkdir = lstdfs.mkdir local lappend = lstdfs.append local lconcat = lstdfs.concat @@ -29,10 +30,18 @@ local ogetenv = os.getenv --指定导出函数 local export_method = nil +--起始列,默认2 +local start_row = 2 --类型定义行,默认2 local type_line = 2 --配置起始行,默认5 local start_line = 5 +--配置表头行,默认start-1 +local head_line = nil +--是否递归 +local recursion = false +--是否导出所有sheet +local allsheet = false --设置utf8 if quanta.platform == "linux" then @@ -120,6 +129,20 @@ local value_func = { end return unserialize('{' .. value .. '}') end, + ["doublearray"] = function(value) + value = sgsub(value, '|', ',') + if sfind(value, '[(]') then + -- 替换'('&')' 为 '{' & '}' + local array = sgsub(value, '[(.*)]', function (s) + return s == '(' and '{' or '}' + end) + return { unserialize(array) } + end + if sfind(value, '[{]') then + return { unserialize(value) } + end + return { unserialize('{' .. value .. '}') } + end, ["sarray"] = function(value) value = sgsub(value, '|', ',') value = sgsub(value, ',', "','") @@ -150,7 +173,7 @@ local function get_sheet_value(sheet, row, col, field_type, header) value = fvalue end if field_type then - local func = value_func[field_type] + local func = value_func[slower(field_type)] if func then return func(value) end @@ -279,7 +302,9 @@ end local function find_sheet_data_struct(sheet) local header = {} local field_type = {} - local head_line = start_line - 1 + if not head_line then + head_line = start_line - 1 + end for col = sheet.first_col, sheet.last_col do -- 读取类型行,作为筛选条件 field_type[col] = get_sheet_value(sheet, type_line, col) @@ -290,29 +315,31 @@ local function find_sheet_data_struct(sheet) end --导出到目标文件 -local function export_sheet_to_output(sheet, output, fname, title) +local function export_sheet_to_output(sheet, output, fname, shname) local header, field_type = find_sheet_data_struct(sheet) if tsize(field_type) == 1 then --未定义数据定义,不导出此sheet - print(sformat("export excel %s sheet %s not need export!", fname, title)) + print(sformat("export excel %s sheet %s not need export!", fname, shname)) return end - --定位起始行 local read_len = start_line local end_line = sheet.last_row - for row = read_len, end_line do - local start_tag = get_sheet_value(sheet, row, 1) - if start_tag == "Start" then - read_len = row - break + if start_row > 1 then + --定位起始行 + for row = read_len, end_line do + local start_tag = get_sheet_value(sheet, row, 1) + if start_tag == "Start" then + read_len = row + break + end end - end - for row = read_len, end_line do - local end_tag = get_sheet_value(sheet, row, 1) - local fkey = get_sheet_value(sheet, row, 2) - if end_tag == "End" or not fkey or fkey == "" then - end_line = row - break + for row = read_len, end_line do + local end_tag = get_sheet_value(sheet, row, 1) + local fkey = get_sheet_value(sheet, row, 2) + if end_tag == "End" or not fkey or fkey == "" then + end_line = row + break + end end end -- 开始处理 @@ -320,7 +347,7 @@ local function export_sheet_to_output(sheet, output, fname, title) for row = read_len, end_line do local record = {} -- 遍历每一列 - for col = 2, sheet.last_col do + for col = start_row, sheet.last_col do -- 过滤掉没有配置的行 if field_type[col] and header[col] then local value = get_sheet_value(sheet, row, col, field_type[col], header[col]) @@ -329,10 +356,12 @@ local function export_sheet_to_output(sheet, output, fname, title) end end end + print(read_len, end_line, row, record) tinsert(records, record) end + local title = allsheet and shname or slower(lstem(fname)) export_method(output, title, fname, build_records(records)) - print(sformat("export file: %s sheet: %s success!", fname, title)) + print(sformat("export file: %s sheet: %s to %s success!", fname, shname, title)) end local function is_excel_file(file) @@ -362,6 +391,9 @@ local function export_excel(input, output) if fullname == output then goto continue end + if not recursion then + goto continue + end local fname = lfilename(fullname) local soutput = lappend(output, fname) lmkdir(soutput) @@ -376,19 +408,23 @@ local function export_excel(input, output) goto continue end local sheets = workbook.sheets() - for _, sheet in pairs(sheets) do + for _, sheet in ipairs(sheets) do local title = slower(sheet.name) if title == "remarks" then print(sformat("export excel %s sheet %s is remarks!", fullname, title)) goto next end - if sheet.last_row < 4 or sheet.last_col <= 0 then + if sheet.last_row < start_line or sheet.last_col <= 0 then print(sformat("export excel %s sheet %s empty!", fullname, title)) goto next end export_sheet_to_output(sheet, output, fname, title) + if not allsheet then + break + end :: next :: end + break end :: continue :: end @@ -421,6 +457,22 @@ local function export_config() if env_staline then start_line = mtointeger(env_staline) end + local env_starow = ogetenv("QUANTA_STAROW") + if env_starow then + start_row = mtointeger(env_starow) + end + local env_headline = ogetenv("QUANTA_HEADLINE") + if env_headline then + head_line = mtointeger(env_headline) + end + local env_recursion = ogetenv("QUANTA_RECURSION") + if env_recursion then + recursion = mtointeger(env_recursion) + end + local env_allsheet = ogetenv("QUANTA_ALLSHEET") + if env_allsheet then + allsheet = mtointeger(env_allsheet) + end local env_format = ogetenv("QUANTA_FORMAT") if env_format == "lua" then export_method = export_records_to_lua