/// <summary> /// 移除所有制表符并转换所有换行符 /// </summary> public void RemoveAllTabAndConvertAllCRLFToLFs() { 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), "提示"); }
/// <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); } } }
/// <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(); }