CSVPosterSettings.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using InABox.Core;
  2. using Inflector;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace InABox.Poster.CSV
  9. {
  10. public class CSVPosterSettings : PosterSettings
  11. {
  12. [FileNameEditor("CSV Files (*.csv)|*.csv", RequireExisting = false)]
  13. public string DefaultOutputFile { get; set; }
  14. public override string DefaultScript(Type TPostable)
  15. {
  16. var tName = TPostable.Name;
  17. var decapital = tName[0..1].ToLower() + tName[1..];
  18. var ns = TPostable.Namespace;
  19. return @"
  20. using " + ns + @";
  21. using InABox.Poster.CSV;
  22. using System.Collections.Generic;
  23. public class Module
  24. {
  25. // Output Results for CSV
  26. public ICSVExport Results { get; set; }
  27. public bool Process(IEnumerable<" + tName + @"> items)
  28. {
  29. // Create new export object. You can use any object as your map,
  30. // but for simple cases just use " + tName + @"
  31. var export = new CSVExport<" + tName + @">();
  32. // Define the mapping from the fields of " + tName + @" to columns in the CSV file.
  33. export.DefineMapping(new()
  34. {
  35. new(""ID"", " + decapital + @" => " + decapital + @".ID),
  36. new(""Column1"", " + decapital + @" => " + decapital + @".Column1),
  37. new(""Column2"", " + decapital + @" => " + decapital + @".Column2),
  38. new(""Column3"", " + decapital + @" => " + decapital + @".Column3)
  39. // etc.
  40. });
  41. foreach(var item in items)
  42. {
  43. // Do processing
  44. export.Add(item);
  45. }
  46. Results = export; // Set result
  47. return true; // return true for success.
  48. }
  49. }";
  50. }
  51. }
  52. }