Feature: MS Excel (xlsx) Import/Export
Microsoft Excel files can be used to import/export devices. Several import bugs were also fixed in the process.
This commit is contained in:
@@ -20,28 +20,23 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
public override string FriendlyValue { get { return friendlyValue; } }
|
||||
public override string FriendlyPreviousValue { get { return friendlyPreviousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
|
||||
{
|
||||
friendlyValue = Value;
|
||||
|
||||
// Validate
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
this.parsedValue = null; // Default = null
|
||||
if (DataReader.TryGetNullableInt(ColumnIndex, out parsedValue))
|
||||
{
|
||||
friendlyValue = parsedValue.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
int valueInt;
|
||||
if (int.TryParse(Value, out valueInt))
|
||||
this.parsedValue = valueInt;
|
||||
else
|
||||
return Error("The Batch Identifier must be a number");
|
||||
return Error("The Batch Identifier must be a number");
|
||||
}
|
||||
|
||||
if (this.parsedValue.HasValue)
|
||||
if (parsedValue.HasValue)
|
||||
{
|
||||
var b = Cache.DeviceBatches.FirstOrDefault(db => db.Id == parsedValue);
|
||||
if (b == null)
|
||||
return Error(string.Format("The identifier ({0}) does not match any Device Batch", Value));
|
||||
friendlyValue = string.Format("{0} [{1}]", b.Name, b.Id);
|
||||
return Error($"The identifier ({friendlyValue}) does not match any Device Batch");
|
||||
friendlyValue = $"{b.Name} [{b.Id}]";
|
||||
}
|
||||
else
|
||||
friendlyValue = null;
|
||||
@@ -55,7 +50,7 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
previousBatch = Cache.DeviceBatches.FirstOrDefault(db => db.Id == ExistingDevice.DeviceBatchId.Value);
|
||||
|
||||
if (previousBatch != null)
|
||||
friendlyPreviousValue = string.Format("{0} [{1}]", previousBatch.Name, previousBatch.Id);
|
||||
friendlyPreviousValue = $"{previousBatch.Name} [{previousBatch.Id}]";
|
||||
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
@@ -65,10 +60,10 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Added ||
|
||||
this.FieldAction == EntityState.Modified)
|
||||
if (FieldAction == EntityState.Added ||
|
||||
FieldAction == EntityState.Modified)
|
||||
{
|
||||
Device.DeviceBatchId = this.parsedValue;
|
||||
Device.DeviceBatchId = parsedValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
@@ -77,30 +72,27 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn && h.Item1.Item1.IndexOf("batch", System.StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
var possibleColumns = Context.Columns
|
||||
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
|
||||
h.Name.IndexOf("batch", StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
|
||||
// All Integers Numbers
|
||||
possibleColumns = possibleColumns.Where(h =>
|
||||
{
|
||||
int lastValue;
|
||||
return Context.RawData.Select(v => v[h.Item2]).Take(100).Where(v => !string.IsNullOrWhiteSpace(v)).All(v => int.TryParse(v, out lastValue));
|
||||
}).ToList();
|
||||
// All Nullable<int> Values
|
||||
possibleColumns = possibleColumns
|
||||
.Where(h => DataReader.TestAllNullableInt(h.Index)).ToList();
|
||||
|
||||
// Multiple Columns, tighten column definition
|
||||
if (possibleColumns.Count() > 1)
|
||||
{
|
||||
possibleColumns = possibleColumns
|
||||
.Where(h =>
|
||||
h.Item1.Item1.IndexOf("batchid", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("batch id", StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
h.Name.IndexOf("batchid", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Name.IndexOf("batch id", StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
}
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user