Как я вызываю ошибку в MSBuild, если файл существует?

Вы можете сделать это несколькими способами. я дам вам способ вывода ячеек из несмежного набора ячеек (называемых областями в модели Excel) в список

    static Excel.Range GetCellsWithValues(Excel.Range row)
    {
        Excel.Range r = null;
        // Cut out unneccessary calcs by using only the intersection of the Row with the UsedRange
        Excel.Range usedRow = row.Application.Intersect(row, row.Worksheet.UsedRange);
        if (usedRow != null)
        {
            foreach (Excel.Range cell in usedRow)
                if (cell.Value2 != null)  //if non-empty unite to r
                    r = (r == null) ? cell : row.Application.Union(r, cell);
        }
        return r;  // a non-contiguous Range will have Areas with blocks of contiguous Ranges
    }

    static List<Excel.Range> GetCellListFromAreas(Excel.Range r)
    {   // this will unwrap the cells from non-contiguous range into a List
        // choose other collections for your use
        List<Excel.Range> cellList = new List<Excel.Range>();
        Excel.Areas areas = r?.Areas;

        if (areas != null)
        { 
            // Unwrap the Areas (blocks of contiguous cells)
            foreach (Excel.Range area in areas)
                foreach (Excel.Range cell in area)
                    cellList.Add(cell);  // add each cell in each contiguous block
        }
        return cellList;
    }

.

Хотя мы предпочитаем, чтобы вы квалифицировали листы явно по имени листа, а не по ActiveSheet.

Также обратите внимание, что вы можете обойтись без Коллекции / Списка диапазонов Excel (ячеек) и поместить значения в массив или что-то еще ...

Итак, очевидно, что вы могли бы сразу поместить в Список и вырезать шаги Союза по диапазонам, чем разворачивать Области. Так как:

    static List<Excel.Range> GetCellListWithValues(Excel.Range row)
    {
        List<Excel.Range> cellList = new List<Excel.Range>();
        Excel.Range r = null;
        Excel.Range usedRow = row.Application.Intersect(row, row.Worksheet.UsedRange);
        if (usedRow != null)
        {
            foreach (Excel.Range cell in usedRow)
                if (cell.Value2 != null)
                    cellList.Add(cell);
        }
        return cellList;
    }
27
задан abatishchev 18 August 2019 в 05:33
поделиться

1 ответ

Как отметил @dprice в своем комментарии, лучшее решение для этого было бы:

<Error Condition="Exists('C:\Process\Fail.txt')" Text="Process did not pass!" /> 
38
ответ дан 28 November 2019 в 05:31
поделиться
Другие вопросы по тегам:

Похожие вопросы: