Add startCell parameter to ConverterToPrompt for enhanced Excel data processing

This commit is contained in:
Ling 2025-02-15 10:41:05 +08:00
parent d1d2bebcd1
commit 89f54544c4
2 changed files with 45 additions and 10 deletions

View File

@ -12,8 +12,9 @@ public class Excel2Prompt
/// </summary>
/// <param name="excelData"></param>
/// <param name="columns"></param>
/// <param name="startCell">A</param>
/// <returns></returns>
public static string ConverterToPrompt(IEnumerable<dynamic> excelData, IEnumerable<string> columns)
public static string ConverterToPrompt(IEnumerable<dynamic> excelData, IEnumerable<string> columns, string startCell)
{
/*
:
@ -36,11 +37,18 @@ public class Excel2Prompt
- Missing values: 7007
*/
var prompts = new List<string>();
prompts.Add("This Excel file contains the following columns:" );
prompts.Add("This Excel file contains the following columns:");
var dataList = excelData.ToList();
// 解析 startCell获取起始列的索引
int startColumnIndex = GetColumnIndexFromCell(startCell);
int columnIndex = startColumnIndex;
foreach (var column in columns)
{
columnIndex++;
string columnLetter = GetExcelColumnName(columnIndex);
var columnData = dataList.Select(row =>
{
var rowDict = (IDictionary<string, object>)row;
@ -50,23 +58,50 @@ public class Excel2Prompt
var nonNullData = columnData.Where(value => value != null).ToList();
var missingValues = columnData.Count - nonNullData.Count;
var uniqueValues = nonNullData.Distinct().Count();
var sampleValues = nonNullData.Take(3).Select(value => value.ToString().Replace("\n"," ")).ToArray();
var sampleValues = nonNullData.Take(3).Select(value => value.ToString().Replace("\n", " ")).ToArray();
// 自动识别数据类型
var dataType = GetColumnDataType(nonNullData);
var prompt = $"- Column '{column.Replace("\n", " ")}'\n" +
$" - Type: {dataType}\n" +
$" - Sample values: ['{string.Join("', '", sampleValues)}']";
//$" - Unique values: {uniqueValues}\n" +
//$" - Missing values: {missingValues}\n";
var prompt = $"- Column '{column.Replace("\n", " ")}' (Excel Column {columnLetter})\n" +
$" - Type: {dataType}\n" +
$" - Sample values: ['{string.Join("', '", sampleValues)}']";
prompts.Add(prompt);
}
return string.Join(Environment.NewLine + Environment.NewLine, prompts);
}
private static int GetColumnIndexFromCell(string cell)
{
// 提取列部分
string columnPart = new string(cell.Where(char.IsLetter).ToArray());
int columnIndex = 0;
int factor = 1;
for (int i = columnPart.Length - 1; i >= 0; i--)
{
columnIndex += (columnPart[i] - 'A' + 1) * factor;
factor *= 26;
}
// 最终结果需要包含StartCell
return columnIndex - 1;
}
private static string GetExcelColumnName(int columnNumber)
{
int dividend = columnNumber;
string columnName = string.Empty;
while (dividend > 0)
{
int modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo) + columnName;
dividend = (dividend - modulo) / 26;
}
return columnName;
}
private static string GetColumnDataType(List<object> data)
{

View File

@ -84,7 +84,7 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel
}
MaxRow = ExcelData.Count();
PromptString = Excel2Prompt.ConverterToPrompt(ExcelData, columns);
PromptString = Excel2Prompt.ConverterToPrompt(ExcelData, columns,StartCell);
}
catch (Exception ex)
{