Ejemplo n.º 1
0
    /// <summary>
    /// 把一条message添加到messageList
    /// </summary>
    /// <param name="message">如果为null 不做处理</param>
    protected void AddMessageToMessageList(DataGridViewConsoleForm.Message message,
                                           ref List <DataGridViewConsoleForm.Message> messageList,
                                           ref bool hasError,
                                           ref bool hasWarning)
    {
        if (message == null)
        {
            return;
        }
        else
        {
            switch (message.Level)
            {
            case DataGridViewConsoleForm.Level.Error:
                hasError = true;
                break;

            case DataGridViewConsoleForm.Level.Warning:
                hasWarning = true;
                break;

            case DataGridViewConsoleForm.Level.Info:
                break;

            default:
                MessageBox.Show(string.Format("意外的Message.Level:({0})", message.Level), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;
            }
            messageList.Add(message);
        }
    }
Ejemplo n.º 2
0
    public static DataGridViewConsoleForm.Message CreateRepeatCellInRowMessage(DataGridViewConsoleForm.Level level, int rowIdx, int[][] repeats)
    {
        DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
        message.Level   = level;
        message.Row     = rowIdx;
        message.Column  = -1;
        message.Caption = GetVerifyMessage(VerifyType.RepeatCellInRow);

        StringBuilder textSb = new StringBuilder("重复的列");

        for (int iRepeats = 0; iRepeats < repeats.Length; iRepeats++)
        {
            textSb.AppendLine();
            int[] repeat = repeats[iRepeats];
            for (int iRepeat = 0; iRepeat < repeat.Length; iRepeat++)
            {
                textSb.Append(ConvertUtility.NumberToLetter(repeat[iRepeat] + 1));
                if (iRepeat < repeat.Length - 1)
                {
                    textSb.Append(", ");
                }
            }
        }
        message.Text = textSb.ToString();
        return(message);
    }
Ejemplo n.º 3
0
 public static DataGridViewConsoleForm.Message CreateTabOrLineBreakMessage(DataGridViewConsoleForm.Level level, int rowIdx, int colIdx, string cellValue)
 {
     DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
     message.Level   = level;
     message.Row     = rowIdx;
     message.Column  = colIdx;
     message.Caption = GetVerifyMessage(VerifyType.TabOrLineBreak);
     message.Text    = string.Format("({0})", cellValue);
     return(message);
 }
Ejemplo n.º 4
0
    private void CopyCsvRowToDataRow(int rowIdx, string[] row, DataRow dataRow)
    {
        int changedCount = 0;

        System.Text.StringBuilder oldSb = new System.Text.StringBuilder();
        System.Text.StringBuilder newSb = new System.Text.StringBuilder();
        for (int iCell = 0; iCell < row.Length; iCell++)
        {
            string iterCell = row[iCell];
            string oldCell  = (string)dataRow[iCell];

            oldSb.Append(string.Format("\"{0}\"", oldCell));
            newSb.Append(string.Format("\"{0}\"", iterCell));
            if (iCell != row.Length - 1)
            {
                oldSb.Append(",");
                newSb.Append(",");
            }

            if (iterCell != oldCell)
            {
                changedCount++;
                CsvEditManager.CellValueChangeItem changeItem = new CsvEditManager.CellValueChangeItem();
                changeItem.Row      = rowIdx;
                changeItem.Column   = iCell;
                changeItem.OldValue = oldCell;
                changeItem.NewValue = iterCell;
                m_CellChangeList.Add(changeItem);

                dataRow[iCell] = iterCell;
            }

            if (string.IsNullOrWhiteSpace(iterCell))
            {
                DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
                message.Level   = DataGridViewConsoleForm.Level.Warning;
                message.Column  = iCell;
                message.Row     = rowIdx;
                message.Caption = "值为空";
                message.Text    = string.Format("源值:\n({0})", oldCell.ToString());
                m_MessageList.Add(message);
            }
        }

        if (changedCount > 0)
        {
            DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
            message.Level   = DataGridViewConsoleForm.Level.Info;
            message.Column  = -1;
            message.Row     = rowIdx;
            message.Caption = string.Format("修改了({0})个单元格", changedCount);
            message.Text    = string.Format("源:\n({0})\n合并后:\n({1})", oldSb.ToString(), newSb.ToString());
            m_MessageList.Add(message);
        }
    }
Ejemplo n.º 5
0
    /// <summary>
    /// 移除所有制表符并转换所有换行符
    /// </summary>
    public void RemoveAllTabAndConvertAllLineBreaks()
    {
        BeforeChangeCellValue();

        List <CsvEditManager.CellValueChangeItem> changeList  = new List <CsvEditManager.CellValueChangeItem>();
        List <DataGridViewConsoleForm.Message>    messageList = new List <DataGridViewConsoleForm.Message>();

        for (int rowIdx = 0; rowIdx < m_DataGridView.Rows.Count; rowIdx++)
        {
            DataGridViewRow iterRow = m_DataGridView.Rows[rowIdx];
            for (int colIdx = 0; colIdx < iterRow.Cells.Count; colIdx++)
            {
                DataGridViewCell iterCell  = iterRow.Cells[colIdx];
                string           iterValue = (string)iterCell.Value;
                if (!string.IsNullOrEmpty(iterValue))
                {
                    string newValue = iterValue.Replace("\t", "");
                    newValue = newValue.Replace("\r\n", "\n");

                    if (iterValue != newValue)
                    {
                        CsvEditManager.CellValueChangeItem change = new CsvEditManager.CellValueChangeItem();
                        change.Row      = rowIdx;
                        change.Column   = colIdx;
                        change.OldValue = iterValue;
                        change.NewValue = newValue;
                        changeList.Add(change);

                        DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
                        message.Level = DataGridViewConsoleForm.Level.Info;
                        int indexOfTab = iterValue.IndexOf('\t');
                        if (indexOfTab > 0 && indexOfTab < iterValue.Length - 1)
                        {
                            message.Level = DataGridViewConsoleForm.Level.Warning;
                        }
                        message.Row     = rowIdx;
                        message.Column  = colIdx;
                        message.Caption = "移除制表符并转换换行符";
                        message.Text    = string.Format("源:\n({0})\n转换后:\n({1})", iterValue, newValue);
                        messageList.Add(message);

                        iterCell.Value = newValue;
                    }
                }
            }
        }
        EditManager.DidCellsValueChange(changeList);

        AfterChangeCellValue();

        DataGridViewConsoleForm.ShowForm(messageList, m_DataGridView, "移除所有制表符并转换所有换行符");
        MessageBox.Show(string.Format("移除所有制表符并转换所有换行符完成, 转换了({0})个单元格", messageList.Count), "提示");
    }
Ejemplo n.º 6
0
 /// <summary>
 /// 非法字符("\t", "\r\n"),csv文件中就不应该有这些字符
 /// </summary>
 public static DataGridViewConsoleForm.Message Verify_TabOrCRLF(DataGridViewConsoleForm.Level level, int rowIdx, int colIdx, string cellValue)
 {
     if (cellValue.Contains('\t') || cellValue.Contains("\r\n"))
     {
         DataGridViewConsoleForm.Message message = CreateMessage(level, rowIdx, colIdx, VerifyType.TabOrCRLF);
         message.Text = string.Format(message.Text, cellValue);
         return(message);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 7
0
 private static DataGridViewConsoleForm.Message CreateMessage(DataGridViewConsoleForm.Level level, int rowIdx, int colIdx, VerifyType verifyType)
 {
     GetVerifyMessage(verifyType, out string messageCaption, out string messageText);
     DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message
     {
         Level   = level,
         Row     = rowIdx,
         Column  = colIdx,
         Caption = messageCaption,
         Text    = messageText
     };
     return(message);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 本地化单元格内容中开头包含占位符"PLACEHOLDER"
 /// </summary>
 public static DataGridViewConsoleForm.Message Verify_Localization_PlaceHolder(DataGridViewConsoleForm.Level level, int rowIdx, int colIdx, string cellValue)
 {
     if (cellValue.Trim().StartsWith("PLACEHOLDER"))
     {
         DataGridViewConsoleForm.Message message = CreateMessage(level, rowIdx, colIdx, VerifyType.Localization_PlaceHolder);
         message.Text = string.Format(message.Text, cellValue);
         return(message);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 9
0
 /// <summary>
 /// 头尾空白字符
 /// </summary>
 public static DataGridViewConsoleForm.Message Verify_HeadAndTailWhiteSpace(DataGridViewConsoleForm.Level level, int rowIdx, int colIdx, string cellValue)
 {
     if (cellValue.Trim() == cellValue)
     {
         return(null);
     }
     else
     {
         DataGridViewConsoleForm.Message message = CreateMessage(level, rowIdx, colIdx, VerifyType.HeadAndTailWhiteSpace);
         message.Text = string.Format(message.Text, cellValue);
         return(message);
     }
 }
Ejemplo n.º 10
0
 /// <summary>
 /// 本地化表第一行第一个单元格应为"String ID"
 /// </summary>
 public static DataGridViewConsoleForm.Message Verify_Localization_FirstCell(DataGridViewConsoleForm.Level level, int rowIdx, int colIdx, string cellValue)
 {
     if (cellValue == "String ID")
     {
         return(null);
     }
     else
     {
         DataGridViewConsoleForm.Message message = CreateMessage(level, rowIdx, colIdx, VerifyType.Localization_FirstCell);
         message.Text = string.Format(message.Text, cellValue);
         return(message);
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 本地化Key不能包含空白字符
 /// </summary>
 public static DataGridViewConsoleForm.Message Verify_Localization_KeyWhiteSpace(DataGridViewConsoleForm.Level level, int rowIdx, string key)
 {
     // 本地化key为空
     if (key.Contains(" ") || key.Trim() != key)
     {
         DataGridViewConsoleForm.Message message = CreateMessage(level, rowIdx, 0, VerifyType.Localization_KeyWhiteSpace);
         message.Text = string.Format(message.Text, key);
         return(message);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 12
0
    /// <summary>
    /// 本地化不能包含重复Key
    /// </summary>
    public static DataGridViewConsoleForm.Message Verify_Localization_RepeatKey(DataGridViewConsoleForm.Level level, string[] keys)
    {
        List <int> excludes = new List <int>();

        // 排除"String ID"
        excludes.Add(0);
        for (int iKey = 1; iKey < keys.Length; iKey++)
        {
            string iterKey = keys[iKey];
            if (string.IsNullOrWhiteSpace(iterKey))
            {
                // 排除空Key
                excludes.Add(iKey);
            }
        }

        int[][] repeats = StringUtility.CheckRepeat(keys, excludes.ToArray());
        if (repeats == null)
        {
            return(null);
        }
        else
        {
            DataGridViewConsoleForm.Message message = CreateMessage(level, -1, 0, VerifyType.Localization_RepeatKey);
            StringBuilder textSb = new StringBuilder(256);
            for (int iRepeats = 0; iRepeats < repeats.Length; iRepeats++)
            {
                int[] repeat = repeats[iRepeats];
                // 重复的行号
                for (int iRepeat = 0; iRepeat < repeat.Length; iRepeat++)
                {
                    textSb.Append(repeat[iRepeat] + 1);
                    if (iRepeat < repeat.Length - 1)
                    {
                        textSb.Append(", ");
                    }
                }
                textSb.Append(string.Format("    Key:({0})", keys[repeat[0]]));
                textSb.AppendLine();
            }
            message.Text = string.Format(message.Text, textSb.ToString());
            return(message);
        }
    }
Ejemplo n.º 13
0
 /// <summary>
 /// key不为空的情况下,未填写文本
 /// </summary>
 public static DataGridViewConsoleForm.Message Verify_Localization_EmptyText(DataGridViewConsoleForm.Level level, int rowIdx, int colIdx, string key, string text)
 {
     if (string.IsNullOrEmpty(key))
     {
         return(null);
     }
     else
     {
         if (string.IsNullOrWhiteSpace(text))
         {
             DataGridViewConsoleForm.Message message = CreateMessage(level, rowIdx, colIdx, VerifyType.Localization_EmptyText);
             return(message);
         }
         else
         {
             return(null);
         }
     }
 }
Ejemplo n.º 14
0
    /// <summary>
    /// 本地化单元格内容去掉换行符后,一段文本重复了两次则验证失败
    /// 检测规则并不严格。例,以下内容是重复的:
    ///		"ABCDABCD"
    ///		"ABC\r\nD\rABCD"
    ///	但以下内容不属于重复的:
    ///		"ABCD ABCD"
    ///		"ABCDxABCD"
    /// 单元格内容的长度小于m的不再检测范围
    ///		m是硬编码写死的,因为这个校验规则并不常用
    ///	Q:为什么会有这个校验规则?
    ///	A:因为新来的策划,不知道怎么操作的,造成了这个错误,为了筛选出有问题的本地化内容,所以添加了这个校验规则
    /// </summary>
    public static DataGridViewConsoleForm.Message Verify_Localization_Special1(DataGridViewConsoleForm.Level level, int rowIdx, int colIdx, string key, string cellValue)
    {
        string formatedValue = cellValue.Replace("\r", "").Replace("\n", "");

        if (formatedValue.Length < 10 || formatedValue.Length % 2 != 0)
        {
            return(null);
        }

        if (formatedValue.Substring(0, formatedValue.Length / 2) == formatedValue.Substring(formatedValue.Length / 2))
        {
            DataGridViewConsoleForm.Message message = CreateMessage(level, rowIdx, colIdx, VerifyType.Localization_Special1);
            message.Caption = string.Format(message.Caption, key);
            message.Text    = string.Format(message.Text, cellValue);
            return(message);
        }
        else
        {
            return(null);
        }
    }
Ejemplo n.º 15
0
 /// <summary>
 /// 本地化Key不能为空
 ///		除非整行都是空
 /// </summary>
 public static DataGridViewConsoleForm.Message Verify_Localization_EmptyKey(DataGridViewConsoleForm.Level level, int rowIdx, DataGridViewRow dataRow, string key)
 {
     // 本地化key为空
     if (string.IsNullOrWhiteSpace(key))
     {
         // 如果这一行所有单元格都为空,就说明这行是分割行
         for (int colIdx = 0; colIdx < dataRow.Cells.Count; colIdx++)
         {
             string cellValue = (string)dataRow.Cells[colIdx].Value;
             if (!string.IsNullOrWhiteSpace(cellValue))
             {
                 DataGridViewConsoleForm.Message message = CreateMessage(level, rowIdx, 0, VerifyType.Localization_EmptyKey);
                 message.Text = string.Format(message.Text, cellValue);
                 return(message);
             }
         }
         return(null);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 16
0
    /// <summary>
    /// 一行内,单元格值重复
    /// </summary>
    /// <param name="exclueds">排除的单元格</param>
    /// <returns>见StringUtility.CheckRepeat</returns>
    public static DataGridViewConsoleForm.Message Verify_RepeatCellInRow(DataGridViewConsoleForm.Level level, int rowIdx, DataGridViewRow dataRow, int[] exclueds)
    {
        string[] strs = new string[dataRow.Cells.Count];
        for (int iCell = 0; iCell < strs.Length; iCell++)
        {
            strs[iCell] = (string)dataRow.Cells[iCell].Value;
        }

        int[][] repeats = StringUtility.CheckRepeat(strs, exclueds);
        if (repeats == null)
        {
            return(null);
        }
        else
        {
            DataGridViewConsoleForm.Message message = CreateMessage(level, rowIdx, -1, VerifyType.RepeatCellInRow);

            StringBuilder textSb = new StringBuilder(256);
            for (int iRepeats = 0; iRepeats < repeats.Length; iRepeats++)
            {
                int[] repeat = repeats[iRepeats];
                // 重复的列号(A,B,C……)
                for (int iRepeat = 0; iRepeat < repeat.Length; iRepeat++)
                {
                    textSb.Append(ConvertUtility.NumberToLetter(repeat[iRepeat] + 1));
                    if (iRepeat < repeat.Length - 1)
                    {
                        textSb.Append(", ");
                    }
                }
                textSb.Append(string.Format("    重复的值:({0})", dataRow.Cells[repeat[0]].Value));
                textSb.AppendLine();
            }
            message.Text = string.Format(message.Text, textSb.ToString());
            return(message);
        }
    }
Ejemplo n.º 17
0
    private void CopyCells(DataGridView dataGridView, Point leftUp, Point rightDown)
    {
        List <DataGridViewConsoleForm.Message> messageList = new List <DataGridViewConsoleForm.Message>();

        // 这里是参考DataGridView.GetClipboardContent()
        DataObject         dataObject = new DataObject();
        string             cellContent = null;
        DataGridViewColumn dataGridViewColumn, nextDataGridViewColumn;
        StringBuilder      sbContent = new StringBuilder(1024);

        int lRowIndex = leftUp.X;
        int uRowIndex = rightDown.X;
        DataGridViewColumn lColumn = dataGridView.Columns[leftUp.Y];
        DataGridViewColumn uColumn = dataGridView.Columns[rightDown.Y];

        Debug.Assert(lRowIndex != -1);
        Debug.Assert(uRowIndex != -1);
        Debug.Assert(lColumn != null);
        Debug.Assert(uColumn != null);
        Debug.Assert(lColumn.Index <= uColumn.Index);
        Debug.Assert(lRowIndex <= uRowIndex);

        // Cycle through the visible rows from lRowIndex to uRowIndex.
        int rowIndex     = lRowIndex;
        int nextRowIndex = -1;

        Debug.Assert(rowIndex != -1);
        while (rowIndex != -1)
        {
            if (rowIndex != uRowIndex)
            {
                nextRowIndex = dataGridView.Rows.GetNextRow(rowIndex, DataGridViewElementStates.Visible);
                Debug.Assert(nextRowIndex != -1);
            }
            else
            {
                nextRowIndex = -1;
            }

            // Cycle through the visible columns from lColumn to uColumn
            dataGridViewColumn = lColumn;
            Debug.Assert(dataGridViewColumn != null);
            while (dataGridViewColumn != null)
            {
                if (dataGridViewColumn != uColumn)
                {
                    nextDataGridViewColumn = dataGridView.Columns.GetNextColumn(dataGridViewColumn, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
                    Debug.Assert(nextDataGridViewColumn != null);
                }
                else
                {
                    nextDataGridViewColumn = null;
                }

                cellContent = (string)dataGridView.Rows.SharedRow(rowIndex).Cells[dataGridViewColumn.Index].Value;

                // 验证单元格内容
                if (!string.IsNullOrEmpty(cellContent))
                {
                    cellContent.Replace("\t", "");

                    if (cellContent[0] == '\"')
                    {
                        DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
                        message.Level   = DataGridViewConsoleForm.Level.Warning;
                        message.Row     = rowIndex;
                        message.Column  = dataGridViewColumn.Index;
                        message.Caption = "第一个字符是(\")";
                        message.Text    = "不支持复制到Excel(可以复制,但是可能会串行错行,或两个单元格被合并为一个)";
                        messageList.Add(message);
                    }

                    if (cellContent.Contains("\n"))
                    {
                        cellContent = cellContent.Replace("\r\n", "\n");
                        cellContent = "\"" + cellContent + "\"";
                    }
                }

                if (nextDataGridViewColumn == null)
                {
                    if (nextRowIndex != -1)
                    {
                        cellContent = cellContent + "\r\n";
                    }
                }
                else
                {
                    cellContent = cellContent + "\t";
                }

                sbContent.Append(cellContent);
                dataGridViewColumn = nextDataGridViewColumn;
            }
            rowIndex = nextRowIndex;
        }
#if DEBUG
        string content = sbContent.ToString();
        Clipboard.SetDataObject(content);
#else
        Clipboard.SetDataObject(sbContent.ToString());
#endif
        DataGridViewConsoleForm.ShowForm(messageList, dataGridView, "复制");
    }
Ejemplo n.º 18
0
    private void Merge(DataTable originalCsv, string[][] changedCsv)
    {
        CsvForm csvForm = MainForm.Instance.GetCsvForm();

        csvForm.BeforeChangeCellValue();

        m_ManyThingList  = new List <CsvEditManager.IUndoRedo>();
        m_CellChangeList = new List <CsvEditManager.CellValueChangeItem>();
        m_MessageList    = new List <DataGridViewConsoleForm.Message>();

        for (int iRowInChangedCSV = 0; iRowInChangedCSV < changedCsv.Length; iRowInChangedCSV++)
        {
            string[] changedDataInRow = changedCsv[iRowInChangedCSV];

            string key = changedDataInRow[0];
            changedDataInRow[0] = key.Trim();
            // 跳过空key
            if (string.IsNullOrWhiteSpace(key))
            {
                continue;
            }
            // 为什么要把Trim的结果赋值回changedDataInRow[0]?
            // 因为changedCSV的key中有可能出现空格或换行符,工具生成的CSV文件中的新增和修改的部分都是直接用changedCSV里的内容,所以需要把changedCSV里的keyTrim一下
            if (key.Trim() == "String ID")
            {
                // 如果key是String ID,则是表头,直接无视
                continue;
            }

            bool existed = false;
            for (int iRowInOriginalCSV = 0; iRowInOriginalCSV < originalCsv.Rows.Count; iRowInOriginalCSV++)
            {
                DataRow originalDataInRow = originalCsv.Rows[iRowInOriginalCSV];
                string  originalKey       = (string)originalDataInRow[0];
                if (originalKey.Trim() == key.Trim())
                {
                    // key前后有空格
                    if (key != key.Trim())
                    {
                        DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message();
                        message.Level   = DataGridViewConsoleForm.Level.Warning;
                        message.Column  = 0;
                        message.Row     = iRowInOriginalCSV;
                        message.Caption = "Key的头尾有空白字符";
                        message.Text    = string.Format("我帮你去除了空格\n去空格前的Key:({0})", key);
                        m_MessageList.Add(message);
                    }

                    existed = true;
                    CopyCsvRowToDataRow(iRowInOriginalCSV, changedDataInRow, originalDataInRow);
                    break;
                }
            }

            if (!existed)
            {
                // 添加新行
                DataRow newRow = originalCsv.NewRow();
                for (int iCell = 0; iCell < changedDataInRow.Length; iCell++)
                {
                    newRow[iCell] = "";
                }
                CsvEditManager.DoAddRowEvent doAddRowEvent = new CsvEditManager.DoAddRowEvent
                {
                    Row = originalCsv.Rows.Count
                };
                m_ManyThingList.Add(doAddRowEvent);

                CopyCsvRowToDataRow(originalCsv.Rows.Count, changedDataInRow, newRow);
                originalCsv.Rows.Add(newRow);
            }
        }

        CsvEditManager.DoCellsValueChangeEvent doCellsValueChangeEvent = new CsvEditManager.DoCellsValueChangeEvent
        {
            ChangeList = m_CellChangeList
        };
        m_ManyThingList.Add(doCellsValueChangeEvent);

        csvForm.EditManager.DidManyThings(m_ManyThingList);
        csvForm.AfterChangeCellValue();
        csvForm.UpdateGridHeader();

        DataGridViewConsoleForm.ShowForm(m_MessageList, csvForm.GetDataGridView(), "本地化合并");
    }
Ejemplo n.º 19
0
    /// <summary>
    /// 把amRow Merge到 originalRow
    /// </summary>
    /// <param name="rowIndexInOriginal">行index,仅用于输出Log</param>
    /// <param name="amRow">AM行</param>
    /// <param name="originalRow">源行</param>
    /// <param name="isNewRow">是不是新增的行</param>
    private void MergeRow(int rowIndexInOriginal, string[] amRow, DataRow originalRow, bool isNewRow = false)
    {
        string originalRowStr   = RowToString(originalRow);
        int    changedCellCount = 0;

        // 魔法数字1:key不需要Merge
        for (int iCell = 1; iCell < amRow.Length; iCell++)
        {
            // 不是要合并的列,跳过
            if (m_MergeCols != null && !m_MergeCols.Contains(iCell))
            {
                continue;
            }

            string iterAMCell       = amRow[iCell];
            string iterOriginalCell = (string)originalRow[iCell];

            // AM文件中为空,跳过
            if (string.IsNullOrWhiteSpace(iterAMCell))
            {
                // 魔法数字1:Text列可以为空
                if (iCell != 1)
                {
                    DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message
                    {
                        Level   = DataGridViewConsoleForm.Level.Warning,
                        Column  = iCell,
                        Row     = rowIndexInOriginal,
                        Caption = "AM文件中,该单元格为空",
                        Text    = string.Format("使用源文件的值:\n({0})", iterOriginalCell)
                    };
                    m_MessageList.Add(message);
                }
            }
            else if (iterAMCell != iterOriginalCell)
            {
                changedCellCount++;
                CSVEditManager.CellValueChangeItem changeItem = new CSVEditManager.CellValueChangeItem
                {
                    Row      = rowIndexInOriginal,
                    Column   = iCell,
                    OldValue = iterOriginalCell,
                    NewValue = iterAMCell
                };
                m_CellChangeList.Add(changeItem);

                originalRow[iCell] = iterAMCell;
            }
        }

        // 添加修改记录,用于显示在控制台
        if (changedCellCount > 0)
        {
            string changedRowStr = RowToString(originalRow);

            if (isNewRow)
            {
                DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message
                {
                    Level   = DataGridViewConsoleForm.Level.Info,
                    Column  = -1,
                    Row     = rowIndexInOriginal,
                    Caption = "合并:新增行",
                    Text    = string.Format("值:\n({0})", changedRowStr)
                };
                m_MessageList.Add(message);
            }
            else
            {
                DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message
                {
                    Level   = DataGridViewConsoleForm.Level.Info,
                    Column  = -1,
                    Row     = rowIndexInOriginal,
                    Caption = string.Format("合并:修改了({0})个单元格", changedCellCount),
                    Text    = string.Format("源值:\n({0})\n合并后:\n({1})", originalRowStr, changedRowStr)
                };
                m_MessageList.Add(message);
            }
        }
    }
Ejemplo n.º 20
0
    /// <summary>
    /// 合并文件
    /// </summary>
    private void MergeCsv(DataTable originalCSV, string[][] amCSV)
    {
        CSVForm csvForm = MainForm.Instance.GetCSVForm();

        csvForm.BeforeChangeCellValue();

        // 做的操作(用于撤销)
        m_DoManyThingList = new List <CSVEditManager.IUndoRedo>();
        m_CellChangeList  = new List <CSVEditManager.CellValueChangeItem>();
        m_MessageList     = new List <DataGridViewConsoleForm.Message>();

        // 遍历AM文件
        for (int iRowInAMCSV = 0; iRowInAMCSV < amCSV.Length; iRowInAMCSV++)
        {
            string[] iterAMRow = amCSV[iRowInAMCSV];
            // 这里Trim是为了匹配到源文件中的Key(Key头尾肯定不能用空白字符)
            string iterAMKey = iterAMRow[0].Trim();

            // 跳过空key,Key为空代表这行为空行
            if (string.IsNullOrWhiteSpace(iterAMKey))
            {
                continue;
            }
            // 如果key是String ID,是表头,直接无视
            if (iterAMKey.ToLower() == "String ID".ToLower())
            {
                continue;
            }
            // key中不能包含空格,跳过
            if (iterAMKey.Contains(" "))
            {
                DataGridViewConsoleForm.Message message = new DataGridViewConsoleForm.Message
                {
                    Level   = DataGridViewConsoleForm.Level.Warning,
                    Column  = 0,
                    Row     = iRowInAMCSV,
                    Caption = CSVEditor.Properties.Resources.MergeLocalizationToolAMCSVKeyContainsSpaceMessageCaption,
                    Text    = string.Format(CSVEditor.Properties.Resources.MergeLocalizationToolAMCSVKeyContainsSpaceMessageText, iterAMKey)
                };
                m_MessageList.Add(message);
                continue;
            }

            // 源文件中是否存在AM文件中的Key
            // 存在的话直接merge,不存在的话,把这一行添加到源文件结尾
            bool amKeyExistedInOriginal = false;
            for (int iRowInOriginalCSV = 0; iRowInOriginalCSV < originalCSV.Rows.Count; iRowInOriginalCSV++)
            {
                DataRow iterOriginalRow = originalCSV.Rows[iRowInOriginalCSV];
                string  iterOriginalKey = (string)iterOriginalRow[0];
                if (iterOriginalKey.Trim() == iterAMKey)
                {
                    amKeyExistedInOriginal = true;
                    MergeRow(iRowInOriginalCSV, iterAMRow, iterOriginalRow);
                    break;
                }
            }

            if (!amKeyExistedInOriginal)
            {
                // 添加新行
                DataRow newRow = originalCSV.NewRow();

                CSVEditManager.DoAddRowEvent doAddRowEvent = new CSVEditManager.DoAddRowEvent
                {
                    Row = originalCSV.Rows.Count
                };
                m_DoManyThingList.Add(doAddRowEvent);

                // Key
                newRow[0] = iterAMKey;
                CSVEditManager.CellValueChangeItem changeItem = new CSVEditManager.CellValueChangeItem
                {
                    Row      = originalCSV.Rows.Count,
                    Column   = 0,
                    OldValue = "",
                    NewValue = iterAMKey
                };
                m_CellChangeList.Add(changeItem);

                // 初始化值
                for (int iCell = 1; iCell < iterAMRow.Length; iCell++)
                {
                    newRow[iCell] = "";
                }

                MergeRow(originalCSV.Rows.Count, iterAMRow, newRow, true);
                originalCSV.Rows.Add(newRow);
            }
        }

        CSVEditManager.DoCellsValueChangeEvent doCellsValueChangeEvent = new CSVEditManager.DoCellsValueChangeEvent
        {
            ChangeList = m_CellChangeList
        };
        m_DoManyThingList.Add(doCellsValueChangeEvent);

        csvForm.EditManager.DidManyThings(m_DoManyThingList);
        csvForm.AfterChangeCellValue();
        csvForm.UpdateGridHeader();

        DataGridViewConsoleForm.ShowForm(m_MessageList, csvForm.GetDataGridView(), "本地化合并");
        Close();
    }