public void Spread(Excel.Worksheet sheet) { _sheet = sheet; sheet.Cells[_titleRow, 1].Value2 = "Value"; if (_token.Empty()) { sheet.Cells[_titleRow + 1, 1].Value2 = "<<empty>>"; } else { _cellDatas = MakeCellData(_sheet, _token).ToList(); //_cellDatas.ForEach(x => x.Value.Spread(x.Cell)); Excel.Range minCell = _sheet.Cells[_cellDatas.Min(x => x.Cell.Row), _cellDatas.Min(x => x.Cell.Column)]; Excel.Range maxCell = _sheet.Cells[_cellDatas.Max(x => x.Cell.Row), _cellDatas.Max(x => x.Cell.Column)]; var rowsCount = maxCell.Row - minCell.Row + 1; var columnsCount = maxCell.Column - minCell.Column + 1; var data = new object[rowsCount, columnsCount]; _cellDatas.ForEach(cellData => { var row = cellData.Cell.Row - minCell.Row; var column = cellData.Cell.Column - minCell.Column; data[row, column] = cellData.Value.ToValue(); }); var range = _sheet.get_Range(minCell.Address, maxCell.Address); range.Value2 = data; } }
public string Read(JObject jo, out bool readenable) // KEEP JSON reader for backwards compatibility. { string errlist = ""; readenable = false; try { JArray ivarja = (JArray)jo["Install"]; if (!ivarja.Empty()) { installationvariables.FromJSONObject(ivarja); } JObject jcond = (JObject)jo["Conditions"]; if (jcond != null) { actioneventlist.FromJSON(jcond); } JObject jprog = (JObject)jo["Programs"]; if (jprog != null) { JArray jf = (JArray)jprog["ProgramSet"]; foreach (JObject j in jf) { ActionProgram ap = new ActionProgram(); string lerr = ap.Read(j); if (lerr.Length == 0) // if can't load, we can't trust the pack, so indicate error so we can let the user manually sort it out { actionprogramlist.Add(ap); } else { errlist += lerr; } } } if (jo["Enabled"] != null) { enabled = (bool)jo["Enabled"]; readenable = true; } //System.Diagnostics.Debug.WriteLine("JSON read enable " + enabled); return(errlist); } catch { return(errlist + " Also Missing JSON fields"); } }
public void Spread(Excel.Worksheet sheet) { _sheet = sheet; sheet.Cells[_titleRow, 1].Value2 = "Value"; if (_token.Empty()) { sheet.Cells[_titleRow + 1, 1].Value2 = "<<empty>>"; } else { _cellDatas = MakeCellData(_sheet, _token).ToList(); _cellDatas.ForEach(x => x.Value.Spread(x.Cell)); } }
private IEnumerable <CellData> MakeCellData(Excel.Worksheet sheet, JArray token) { if (token.Empty()) { return(new List <CellData>()); } else { return(_token.Select((x, i) => new { Index = i, Cell = (Excel.Range)sheet?.Cells[_titleRow + i + 1, 1], JToken = x.CreateJsonToken(), }) .Select(x => new CellData { Type = DataType.Value, Index = x.Index, Cell = x.Cell, Key = null, Value = x.JToken, })); } }
private static JObject ConvertFromEDSMBodies(JObject jo) // protect yourself against bad JSON { JObject jout = new JObject { ["timestamp"] = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture), ["event"] = "Scan", ["EDDFromEDSMBodie"] = true, ["BodyName"] = jo["name"], }; if (jo["discovery"] != null && jo["discovery"].HasValues) // much more defense around this.. EDSM gives discovery=null back { jout["discovery"] = jo["discovery"]; } if (jo["orbitalInclination"] != null) { jout["OrbitalInclination"] = jo["orbitalInclination"]; } if (jo["orbitalEccentricity"] != null) { jout["Eccentricity"] = jo["orbitalEccentricity"]; } if (jo["argOfPeriapsis"] != null) { jout["Periapsis"] = jo["argOfPeriapsis"]; } if (jo["semiMajorAxis"].Double() != 0) { jout["SemiMajorAxis"] = jo["semiMajorAxis"].Double() * JournalScan.oneAU_m; // AU -> metres } if (jo["orbitalPeriod"].Double() != 0) { jout["OrbitalPeriod"] = jo["orbitalPeriod"].Double() * JournalScan.oneDay_s; // days -> seconds } if (jo["rotationalPeriodTidallyLocked"] != null) { jout["TidalLock"] = jo["rotationalPeriodTidallyLocked"]; } if (jo["axialTilt"] != null) { jout["AxialTilt"] = jo["axialTilt"].Double() * Math.PI / 180.0; // degrees -> radians } if (jo["rotationalPeriod"].Double() != 0) { jout["RotationalPeriod"] = jo["rotationalPeriod"].Double() * JournalScan.oneDay_s; // days -> seconds } if (jo["surfaceTemperature"] != null) { jout["SurfaceTemperature"] = jo["surfaceTemperature"]; } if (jo["distanceToArrival"] != null) { jout["DistanceFromArrivalLS"] = jo["distanceToArrival"]; } if (jo["parents"] != null) { jout["Parents"] = jo["parents"]; } if (jo["id64"] != null) { jout["BodyID"] = jo["id64"].Long() >> 55; } if (!jo["type"].Empty()) { if (jo["type"].Value <string>().Equals("Star")) { jout["StarType"] = EDSMStar2JournalName(jo["subType"].StrNull()); // pass thru null to code, it will cope with it jout["Age_MY"] = jo["age"]; jout["StellarMass"] = jo["solarMasses"]; jout["Radius"] = jo["solarRadius"].Double() * JournalScan.solarRadius_m; // solar-rad -> metres } else if (jo["type"].Value <string>().Equals("Planet")) { jout["Landable"] = jo["isLandable"]; jout["MassEM"] = jo["earthMasses"]; jout["Volcanism"] = jo["volcanismType"]; string atmos = jo["atmosphereType"].StrNull(); if (atmos != null && atmos.IndexOf("atmosphere", StringComparison.InvariantCultureIgnoreCase) == -1) { atmos += " atmosphere"; } jout["Atmosphere"] = atmos; jout["Radius"] = jo["radius"].Double() * 1000.0; // km -> metres jout["PlanetClass"] = EDSMPlanet2JournalName(jo["subType"].Str()); if (jo["terraformingState"] != null) { jout["TerraformState"] = jo["terraformingState"]; } if (jo["surfacePressure"] != null) { jout["SurfacePressure"] = jo["surfacePressure"].Double() * JournalScan.oneAtmosphere_Pa; // atmospheres -> pascals } if (jout["TerraformState"].Str() == "Candidate for terraforming") { jout["TerraformState"] = "Terraformable"; } } } JArray rings = (jo["belts"] ?? jo["rings"]) as JArray; if (!rings.Empty()) { JArray jring = new JArray(); foreach (JObject ring in rings) { jring.Add(new JObject { ["InnerRad"] = ring["innerRadius"].Double() * 1000, ["OuterRad"] = ring["outerRadius"].Double() * 1000, ["MassMT"] = ring["mass"], ["RingClass"] = ring["type"], ["Name"] = ring["name"] }); } jout["Rings"] = jring; } if (!jo["materials"].Empty()) // Check if materials has null { Dictionary <string, double?> mats; Dictionary <string, double> mats2; mats = jo["materials"]?.ToObjectProtected <Dictionary <string, double?> >(); mats2 = new Dictionary <string, double>(); foreach (string key in mats.Keys) { if (mats[key] == null) { mats2[key.ToLowerInvariant()] = 0.0; } else { mats2[key.ToLowerInvariant()] = mats[key].Value; } } jout["Materials"] = JObject.FromObject(mats2); } return(jout); }
public static JObject ConvertFromEDSMBodies(JObject jo) { JObject jout = new JObject { ["timestamp"] = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture), ["event"] = "Scan", ["EDDFromEDSMBodie"] = true, ["BodyName"] = jo["name"], }; if (jo["orbitalInclination"] != null) { jout["OrbitalInclination"] = jo["orbitalInclination"]; } if (jo["orbitalEccentricity"] != null) { jout["Eccentricity"] = jo["orbitalEccentricity"]; } if (jo["argOfPeriapsis"] != null) { jout["Periapsis"] = jo["argOfPeriapsis"]; } if (jo["semiMajorAxis"].Double() != 0) { jout["SemiMajorAxis"] = jo["semiMajorAxis"].Double() * JournalScan.oneAU_m; // AU -> metres } if (jo["orbitalPeriod"].Double() != 0) { jout["OrbitalPeriod"] = jo["orbitalPeriod"].Double() * JournalScan.oneDay_s; // days -> seconds } if (jo["rotationalPeriodTidallyLocked"] != null) { jout["TidalLock"] = jo["rotationalPeriodTidallyLocked"]; } if (jo["axialTilt"] != null) { jout["AxialTilt"] = jo["axialTilt"].Double() * Math.PI / 180.0; // degrees -> radians } if (jo["rotationalPeriod"].Double() != 0) { jout["RotationalPeriod"] = jo["rotationalPeriod"].Double() * JournalScan.oneDay_s; // days -> seconds } if (jo["surfaceTemperature"] != null) { jout["SurfaceTemperature"] = jo["surfaceTemperature"]; } if (!jo["type"].Empty()) { if (jo["type"].Value <string>().Equals("Star")) { jout["StarType"] = EDSMStar2JournalName(jo["subType"].Str()); jout["Age_MY"] = jo["age"]; jout["StellarMass"] = jo["solarMasses"]; jout["Radius"] = jo["solarRadius"].Double() * JournalScan.solarRadius_m; // solar-rad -> metres } else if (jo["type"].Value <string>().Equals("Planet")) { jout["Landable"] = jo["isLandable"]; jout["MassEM"] = jo["earthMasses"]; jout["Volcanism"] = jo["volcanismType"]; jout["Atmosphere"] = jo["atmosphereType"]; jout["Radius"] = jo["radius"].Double() * 1000.0; // km -> metres jout["PlanetClass"] = EDSMPlanet2JournalName(jo["subType"].Str()); if (jo["terraformingState"] != null) { jout["TerraformState"] = jo["terraformingState"]; } if (jo["surfacePressure"] != null) { jout["SurfacePressure"] = jo["surfacePressure"].Double() * 101325; // atmospheres -> pascals } if (jout["TerraformState"].Str() == "Candidate for terraforming") { jout["TerraformState"] = "Terraformable"; } } } JArray rings = (jo["belts"] ?? jo["rings"]) as JArray; if (!rings.Empty()) { JArray jring = new JArray(); foreach (JObject ring in rings) { jring.Add(new JObject { ["InnerRad"] = ring["innerRadius"].Double() * 1000, ["OuterRad"] = ring["outerRadius"].Double() * 1000, ["MassMT"] = ring["mass"], ["RingClass"] = ring["type"], ["Name"] = ring["name"] }); } jout["Rings"] = jring; } if (!jo["Materials"].Empty()) // Check if matieals has null { Dictionary <string, double?> mats; Dictionary <string, double> mats2; mats = jo["Materials"]?.ToObject <Dictionary <string, double?> >(); mats2 = new Dictionary <string, double>(); foreach (string key in mats.Keys) { if (mats[key] == null) { mats2[key.ToLower()] = 0.0; } else { mats2[key.ToLower()] = mats[key].Value; } } jout["Materials"] = JObject.FromObject(mats2); } return(jout); }