Skip to content

Commit

Permalink
copy to unity project
Browse files Browse the repository at this point in the history
  • Loading branch information
cathei committed Jun 18, 2024
1 parent 5db159e commit 0331a37
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,24 @@ public ExcelSheetConverter(string loadPath, TimeZoneInfo timeZoneInfo = null, st
_extension = extension;
_fileSystem = fileSystem ?? new FileSystem();
_pages = new Dictionary<string, List<Page>>();

#if NET5_0_OR_GREATER
// https://github.com/ExcelDataReader/ExcelDataReader?tab=readme-ov-file#important-note-on-net-core
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif
}

private class Page : IRawSheetImporterPage
{
private DataTable _table;
private IFormatProvider _formatProvider;

public string SubName { get; }

public Page(DataTable table, string subName)
public Page(DataTable table, string subName, IFormatProvider formatProvider)
{
_table = table;
_formatProvider = formatProvider;
SubName = subName;
}

Expand All @@ -45,7 +52,7 @@ public string GetCell(int col, int row)
if (col >= _table.Columns.Count || row >= _table.Rows.Count)
return null;

return _table.Rows[row][col].ToString();
return Convert.ToString(_table.Rows[row][col], _formatProvider);
}
}

Expand Down Expand Up @@ -90,7 +97,7 @@ protected override Task<bool> LoadData()
_pages.Add(sheetName, sheetList);
}

sheetList.Add(new Page(table, subName));
sheetList.Add(new Page(table, subName, FormatProvider));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected override async Task<bool> LoadData()
}))
{
var sheetReq = service.Spreadsheets.Get(_gsheetAddress);
sheetReq.Fields = "properties,sheets(properties,data.rowData.values.formattedValue)";
sheetReq.Fields = "sheets(properties.title,data.rowData.values.formattedValue)";
_spreadsheet = await sheetReq.ExecuteAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class AssetPath : ISheetAssetPath

public virtual string BasePath => string.Empty;
public virtual string Extension => string.Empty;
public virtual string DirectorySeparator => "/";

[Preserve]
public AssetPath(string rawValue)
Expand All @@ -29,7 +30,19 @@ public AssetPath(string rawValue)
if (!string.IsNullOrEmpty(Extension))
filePath = $"{filePath}.{Extension}";

FullPath = Path.Combine(BasePath, filePath);
FullPath = CombinePath(BasePath, filePath, DirectorySeparator);
}

// Defining our own, since Path.Combine or similar methods does not support custom separator,
public static string CombinePath(string basePath, string filePath, string separator)
{
if (string.IsNullOrEmpty(basePath))
return filePath;

if (basePath.EndsWith(separator))
return $"{basePath}{filePath}";

return $"{basePath}{separator}{filePath}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public partial class AddressablePath : ISheetAssetPath

public virtual string BasePath => string.Empty;
public virtual string Extension => string.Empty;
public virtual string DirectorySeparator => "/";

public AddressablePath(string rawValue)
{
Expand All @@ -35,7 +36,7 @@ public AddressablePath(string rawValue)
if (!string.IsNullOrEmpty(Extension))
filePath = $"{filePath}.{Extension}";

FullPath = Path.Combine(BasePath, filePath);
FullPath = AssetPath.CombinePath(BasePath, filePath, DirectorySeparator);
SubAssetName = subAssetName;

if (!string.IsNullOrEmpty(SubAssetName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class DirectAssetPath : ISheetAssetPath

public virtual string BasePath => "Assets";
public virtual string Extension => string.Empty;
public virtual string DirectorySeparator => "/";

public DirectAssetPath(string rawValue)
{
Expand All @@ -39,7 +40,7 @@ public DirectAssetPath(string rawValue)
if (!string.IsNullOrEmpty(Extension))
filePath = $"{filePath}.{Extension}";

FullPath = Path.Combine(BasePath, filePath);
FullPath = AssetPath.CombinePath(BasePath, filePath, DirectorySeparator);
SubAssetName = subAssetName;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public partial class ResourcePath : ISheetAssetPath
public string SubAssetName { get; }

public virtual string BasePath => string.Empty;
public virtual string DirectorySeparator => "/";

public ResourcePath(string rawValue)
{
Expand All @@ -31,7 +32,7 @@ public ResourcePath(string rawValue)
var filePath = match.Groups[1].Value;
var subAssetName = match.Groups[2].Value;

FullPath = Path.Combine(BasePath, filePath);
FullPath = AssetPath.CombinePath(BasePath, filePath, DirectorySeparator);
SubAssetName = subAssetName;
}
}
Expand Down

0 comments on commit 0331a37

Please sign in to comment.