// // Copyright 2026 The InfiniFlow Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // package parser import ( "bytes" "fmt" "strings" "github.com/xuri/excelize/v2" ) type XLSXParser struct { libType string ParseMethod string OutputFormat string TCADPAPIServer string TCADPAPIKey string TCADPTableResultType string TCADPMarkdownImageResponseType string } func NewXLSXParser(libType string) (*XLSXParser, error) { if libType == "" { libType = "excelize" } return &XLSXParser{ libType: libType, TCADPTableResultType: "1", TCADPMarkdownImageResponseType: "1", }, nil } func (p *XLSXParser) String() string { return "XLSXParser" } func (p *XLSXParser) ConfigureFromSetup(setup map[string]any) { if p == nil || setup == nil { return } if v, ok := setup["parse_method"].(string); ok && v != "" { p.ParseMethod = v } if v, ok := setup["output_format"].(string); ok && v != "" { p.OutputFormat = v } if v, ok := setup["tcadp_apiserver"].(string); ok && v != "" { p.TCADPAPIServer = v } if v, ok := setup["tcadp_api_key"].(string); ok { p.TCADPAPIKey = v } if v, ok := setup["table_result_type"].(string); ok && v != "" { p.TCADPTableResultType = v } if v, ok := setup["markdown_image_response_type"].(string); ok && v != "" { p.TCADPMarkdownImageResponseType = v } } func normalizeXLSXParseMethod(raw string) string { method := strings.ToLower(strings.TrimSpace(raw)) if method == "tcadp parser" { return "tcadp" } return method } func (p *XLSXParser) ParseWithResult(filename string, data []byte) ParseResult { method := normalizeXLSXParseMethod(p.ParseMethod) switch method { case "tcadp": return parseSpreadsheetWithTCADP( filename, data, "XLSX", p.TCADPAPIServer, p.TCADPAPIKey, p.TCADPTableResultType, p.TCADPMarkdownImageResponseType, p.OutputFormat, ) case "", "excelize": // Continue with the local Excelize parser. default: return ParseResult{ Err: fmt.Errorf("unsupported XLSX parse method: %q", p.ParseMethod), } } f, err := excelize.OpenReader(bytes.NewReader(data)) if err != nil { return ParseResult{Err: fmt.Errorf("xlsx open: %w", err)} } defer f.Close() sheets := f.GetSheetList() var html strings.Builder html.WriteString("") for _, sheet := range sheets { html.WriteString("

") html.WriteString(sheet) html.WriteString("

") rows, err := f.GetRows(sheet) if err != nil { continue } html.WriteString("") for _, row := range rows { html.WriteString("") for _, cell := range row { html.WriteString("") } html.WriteString("") } html.WriteString("
") html.WriteString(htmlEscape(cell)) html.WriteString("
") } html.WriteString("") return ParseResult{ OutputFormat: "html", File: map[string]any{"name": filename, "format": "xlsx", "sheets": len(sheets)}, HTML: html.String(), } }