-1

所以 atm 我正在将一个 excel 文档导入我的项目中。然后,如果电子表格中的任何列为空,我将使用 if 语句显示相应的错误消息。我的代码正在运行,但它看起来并不整洁。无论如何将所有 if 语句重构为循环以显示错误消息,可能是另一种检查所有的方法?checkIfColumnIsEmpty 是一个 bool 方法,如果该列为空,则返回 true

//if either column 0 and 1 are empty && column 2,3,4 and 5 are not
    if (!checkIfColumnisEmpty(r.ItemArray[0]) || !checkIfColumnisEmpty(r.ItemArray[1])
        && checkIfColumnisEmpty(r.ItemArray[2]) && checkIfColumnisEmpty(r.ItemArray[3])
         && checkIfColumnisEmpty(r.ItemArray[4]) && checkIfColumnisEmpty(r.ItemArray[5]))
    {
        if (checkIfColumnisEmpty(r.ItemArray[0]) && !checkIfColumnisEmpty(r.ItemArray[1]))
        {
            throw new ImportBOQException("Error importing document: First column is empty");
        }
        else if (!checkIfColumnisEmpty(r.ItemArray[0]) && checkIfColumnisEmpty(r.ItemArray[1]))
        {
            throw new ImportBOQException("Error importing document: Second column is empty");
        }

        else if (!checkIfColumnisEmpty(r.ItemArray[0]) && !checkIfColumnisEmpty(r.ItemArray[1]))
        {
            //all columns are valid so...
            Column0inSpreadsheet = r.ItemArray[0] as string;
            Column1inSpreadsheet = r.ItemArray[1] as string;

          //Other code which performs other operations, once the level as reached this far
        }
    }                                

    //if column 0 and 1 are NOT empty && Either column 2,3,4 or 5 is empty
    else if (checkIfColumnisEmpty(r.ItemArray[0]) && checkIfColumnisEmpty(r.ItemArray[1])
          || !checkIfColumnisEmpty(r.ItemArray[2]) || !checkIfColumnisEmpty(r.ItemArray[3])
           || !checkIfColumnisEmpty(r.ItemArray[4]) || !checkIfColumnisEmpty(r.ItemArray[5]))
    {
        if (checkIfColumnisEmpty(r.ItemArray[2]))
        {
            throw new ImportBOQException("Error importing document: Third column is empty");
        }
        else if (checkIfColumnisEmpty(r.ItemArray[3]))
        {
            throw new ImportBOQException("Error importing document: Fourth column is empty");
        }
        else if (checkIfColumnisEmpty(r.ItemArray[4]))
        {
            throw new ImportBOQException("Error importing document: Fifth column is empty");
        }
        else if (checkIfColumnisEmpty(r.ItemArray[5]))
        {
            throw new ImportBOQException("Error importing document: Sixth column is empty");
        }
        else
        //all columns are valid so...
        {   Column2inSpreadsheet = (r.ItemArray[2]) as string;
            Column3inSpreadsheet = (r.ItemArray[3]) as string;
            Column4inSpreadsheet = (r.ItemArray[4]) as string;
            Column5inSpreadsheet = (r.ItemArray[5]) as string;

            //Other code which performs other operations, once the level as reached this far
        }
    }
    else
    //other errors ot related to empty colums
    {
        throw new Exception("Error Uploading");
    }
}
4

2 回答 2

1

首先,将 r 转换为具有命名属性的类,而不是使用数组索引器并从源初始化集合。其次,使用签名 bool VerifyColumn(string value, string propertyName) 创建辅助方法。这是一个基础重构,也许您可​​以将您的逻辑从“Holy s***”更改为“那是一团糟,但我们可以改变它!”

于 2013-01-21T12:26:46.947 回答
1

如果您只需要检查其中一列是否为“空”并获取它的索引,您可以使用它:

var firstEmptyColumn = r.ItemArray
    .Select((f, i) => new { Field=f, Index=i })
    .FirstOrDefault(x => r.IsNull(x.Index) || x.Field.ToString().Trim().Length == 0);
if (firstEmptyColumn != null)
{
    string errorMsg = string.Format("Error importing document: {0} column is empty",
        firstEmptyColumn.Index);
    throw new ImportBOQException(errorMsg);
}
于 2013-01-21T12:31:20.383 回答