private static int UpdateWorksheetRowFromMembershipData(SharedStringTable sharedStrings, Row row, List <MembershipData> inputDataList) { int updatedRows = 0; var cellValue = string.Empty; var collumnIndex = string.Empty; var rowIndex = row.RowIndex.ToString(); try { var currentMembershipData = new MembershipData(); currentMembershipData.CloneExcelColumnIndexInAllProperties(inputDataList.FirstOrDefault()); // First collecting current old data for each cell for update // into currentMembershipData to verify new and old values next foreach (var cell in row.Descendants <Cell>()) { cellValue = collumnIndex = string.Empty; GetCellValueAndColumn(sharedStrings, row, cell, out cellValue, out collumnIndex); // Update Excel Values for each inputData record currentMembershipData.UpdateExcelCellOldValueInAllProperties(collumnIndex, cellValue); } ; // Next copy all CSV New values from inputData list that match the same email inputDataList.ForEach(m => { if (m.Email.CsvNewValue.Equals(currentMembershipData.Email.ExcelCellOldValue, StringComparison.InvariantCultureIgnoreCase)) { currentMembershipData.CloneCsvNewValueInAllProperties(m); } }); // If no new value in email, nothing to update if (string.IsNullOrEmpty(currentMembershipData.Email.CsvNewValue)) { return(updatedRows); } // Update Exists property for the current record inputDataList.Where(m => m.Email.CsvNewValue.Equals(currentMembershipData.Email.CsvNewValue, StringComparison.InvariantCultureIgnoreCase)) .SingleOrDefault().ExistsInExcelFile = true; // Finally verify if we need to update that record if (currentMembershipData.ContainsAnyNotMatchingOldAndNewValues()) { if (TryUpdateRow(sharedStrings, row, currentMembershipData)) { updatedRows++; } } } catch (Exception ex) { Console.WriteLine($"\nException detected during updating cell value '{cellValue}' " + $"of column Index '{collumnIndex}' and row Index '{rowIndex}'. {ex}"); throw; } return(updatedRows); }
public void ContainsNotMatchingOldAndNewValues_WithoutExcelOldValues_Success_Test() { var testRecord = new MembershipData(); testRecord.FirstName.CsvNewValue = "testFirstName"; testRecord.LastName.CsvNewValue = "testLastName"; testRecord.Email.CsvNewValue = "testEmail"; Assert.IsTrue(testRecord.ContainsAnyNotMatchingOldAndNewValues()); }