Add startCell parameter to ConverterToPrompt for enhanced Excel data processing
This commit is contained in:
parent
d1d2bebcd1
commit
89f54544c4
|
@ -12,8 +12,9 @@ public class Excel2Prompt
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="excelData"></param>
|
/// <param name="excelData"></param>
|
||||||
/// <param name="columns"></param>
|
/// <param name="columns"></param>
|
||||||
|
/// <param name="startCell">A</param>
|
||||||
/// <returns></returns>
|
/// <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
|
- Missing values: 7007
|
||||||
*/
|
*/
|
||||||
var prompts = new List<string>();
|
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();
|
var dataList = excelData.ToList();
|
||||||
|
|
||||||
|
// 解析 startCell,获取起始列的索引
|
||||||
|
int startColumnIndex = GetColumnIndexFromCell(startCell);
|
||||||
|
|
||||||
|
int columnIndex = startColumnIndex;
|
||||||
foreach (var column in columns)
|
foreach (var column in columns)
|
||||||
{
|
{
|
||||||
|
columnIndex++;
|
||||||
|
string columnLetter = GetExcelColumnName(columnIndex);
|
||||||
|
|
||||||
var columnData = dataList.Select(row =>
|
var columnData = dataList.Select(row =>
|
||||||
{
|
{
|
||||||
var rowDict = (IDictionary<string, object>)row;
|
var rowDict = (IDictionary<string, object>)row;
|
||||||
|
@ -50,23 +58,50 @@ public class Excel2Prompt
|
||||||
var nonNullData = columnData.Where(value => value != null).ToList();
|
var nonNullData = columnData.Where(value => value != null).ToList();
|
||||||
var missingValues = columnData.Count - nonNullData.Count;
|
var missingValues = columnData.Count - nonNullData.Count;
|
||||||
var uniqueValues = nonNullData.Distinct().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 dataType = GetColumnDataType(nonNullData);
|
||||||
|
|
||||||
var prompt = $"- Column '{column.Replace("\n", " ")}'\n" +
|
var prompt = $"- Column '{column.Replace("\n", " ")}' (Excel Column {columnLetter})\n" +
|
||||||
$" - Type: {dataType}\n" +
|
$" - Type: {dataType}\n" +
|
||||||
$" - Sample values: ['{string.Join("', '", sampleValues)}']";
|
$" - Sample values: ['{string.Join("', '", sampleValues)}']";
|
||||||
//$" - Unique values: {uniqueValues}\n" +
|
|
||||||
//$" - Missing values: {missingValues}\n";
|
|
||||||
|
|
||||||
prompts.Add(prompt);
|
prompts.Add(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
return string.Join(Environment.NewLine + Environment.NewLine, prompts);
|
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)
|
private static string GetColumnDataType(List<object> data)
|
||||||
{
|
{
|
||||||
|
|
|
@ -84,7 +84,7 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel
|
||||||
}
|
}
|
||||||
MaxRow = ExcelData.Count();
|
MaxRow = ExcelData.Count();
|
||||||
|
|
||||||
PromptString = Excel2Prompt.ConverterToPrompt(ExcelData, columns);
|
PromptString = Excel2Prompt.ConverterToPrompt(ExcelData, columns,StartCell);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue