Skip to content

Commit a0dd725

Browse files
committed
--column-names-row, --data-start-row をテーブル個別指定できるように
1 parent 0803d65 commit a0dd725

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

Readme.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,17 @@ xlsx -> yaml (from) 変換で出力するseedファイルをID単位ごとに分
188188

189189
分割しておくと、複数人で同時に別のIDのデータを編集してコミットする等の場合に、コンフリクトがおこりにくくなります。
190190

191-
`(前削除桁数:)(xlsxファイル名/)seedテーブル名(:後削除桁数)(@fromまたは@to)(@key=プライマリキー名)`の形式で指定します。(複数指定はコマンドラインでは`,`区切り・GUIでは改行区切り)
191+
`(前削除桁数:)(xlsxファイル名/)seedテーブル名(:後削除桁数)(@その他のオプション)`の形式で指定します。(複数指定はコマンドラインでは`,`区切り・GUIでは改行区切り)
192192

193193
xlsxファイル名を指定する場合はパスを含まずファイル名のみ(path/to/foo.xlsxではなくfoo.xlsx)で指定してください。
194194

195+
その他のオプション:
196+
197+
- @fromまたは@to: それぞれxlsx -> yaml (from) 変換、yaml -> xlsx (to) 変換のみでそのオプションが有効になります
198+
- @key=プライマリキー名: プライマリキーを"id"以外にします
199+
- @column-names-row=数値: --column-names-row をテーブル個別指定
200+
- @data-start-row=数値: --data-start-row をテーブル個別指定
201+
195202
ID(プライマリキーを指定した場合はそのカラム)数値の桁を上の方から前削除桁数分、下の方から後削除桁数分削ったものがファイル名として使われます。
196203

197204
例:

seedtable-test/SubdivideOptionTest.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,35 @@
1111
namespace seedtable_test {
1212
public class SubdivideOptionTest {
1313
public static IEnumerable<object[]> Examples() {
14-
yield return new object[] { "foos", "NO_NAME", "foos", false, 0, 0, "id", OnOperation.From | OnOperation.To };
15-
yield return new object[] { "foo.xlsx/foos", "foo.xlsx", "foos", false, 0, 0, "id", OnOperation.From | OnOperation.To };
16-
yield return new object[] { "foos:0", "NO_NAME", "foos", true, 0, 0, "id", OnOperation.From | OnOperation.To };
17-
yield return new object[] { "0:foos", "NO_NAME", "foos", true, 0, 0, "id", OnOperation.From | OnOperation.To };
18-
yield return new object[] { "foos:1", "NO_NAME", "foos", true, 0, 1, "id", OnOperation.From | OnOperation.To };
19-
yield return new object[] { "1:foos", "NO_NAME", "foos", true, 1, 0, "id", OnOperation.From | OnOperation.To };
20-
yield return new object[] { "2:foos:2", "NO_NAME", "foos", true, 2, 2, "id", OnOperation.From | OnOperation.To };
21-
yield return new object[] { "foos@NO_OPTION", "NO_NAME", "foos", false, 0, 0, "id", OnOperation.From | OnOperation.To };
22-
yield return new object[] { "foos@from", "NO_NAME", "foos", false, 0, 0, "id", OnOperation.From };
23-
yield return new object[] { "foos@TO", "NO_NAME", "foos", false, 0, 0, "id", OnOperation.To };
24-
yield return new object[] { "foos@key=foo_id", "NO_NAME", "foos", false, 0, 0, "foo_id", OnOperation.From | OnOperation.To };
25-
yield return new object[] { "foos@from@key=foo_id", "NO_NAME", "foos", false, 0, 0, "foo_id", OnOperation.From };
26-
yield return new object[] { "1:foo.xlsx/foos:0@from@key=foo_id", "foo.xlsx", "foos", true, 1, 0, "foo_id", OnOperation.From };
14+
yield return new object[] { "foos", "NO_NAME", "foos", false, 0, 0, "id", null, null, OnOperation.From | OnOperation.To };
15+
yield return new object[] { "foo.xlsx/foos", "foo.xlsx", "foos", false, 0, 0, "id", null, null, OnOperation.From | OnOperation.To };
16+
yield return new object[] { "foos:0", "NO_NAME", "foos", true, 0, 0, "id", null, null, OnOperation.From | OnOperation.To };
17+
yield return new object[] { "0:foos", "NO_NAME", "foos", true, 0, 0, "id", null, null, OnOperation.From | OnOperation.To };
18+
yield return new object[] { "foos:1", "NO_NAME", "foos", true, 0, 1, "id", null, null, OnOperation.From | OnOperation.To };
19+
yield return new object[] { "1:foos", "NO_NAME", "foos", true, 1, 0, "id", null, null, OnOperation.From | OnOperation.To };
20+
yield return new object[] { "2:foos:2", "NO_NAME", "foos", true, 2, 2, "id", null, null, OnOperation.From | OnOperation.To };
21+
yield return new object[] { "foos@NO_OPTION", "NO_NAME", "foos", false, 0, 0, "id", null, null, OnOperation.From | OnOperation.To };
22+
yield return new object[] { "foos@from", "NO_NAME", "foos", false, 0, 0, "id", null, null, OnOperation.From };
23+
yield return new object[] { "foos@TO", "NO_NAME", "foos", false, 0, 0, "id", null, null, OnOperation.To };
24+
yield return new object[] { "foos@key=foo_id", "NO_NAME", "foos", false, 0, 0, "foo_id", null, null, OnOperation.From | OnOperation.To };
25+
yield return new object[] { "foos@column-names-row=9", "NO_NAME", "foos", false, 0, 0, "id", 9, null, OnOperation.From | OnOperation.To };
26+
yield return new object[] { "foos@data-start-row=10", "NO_NAME", "foos", false, 0, 0, "id", null, 10, OnOperation.From | OnOperation.To };
27+
yield return new object[] { "foos@from@key=foo_id", "NO_NAME", "foos", false, 0, 0, "foo_id", null, null, OnOperation.From };
28+
yield return new object[] { "1:foo.xlsx/foos:0@from@key=foo_id@column-names-row=9", "foo.xlsx", "foos", true, 1, 0, "foo_id", 9, null, OnOperation.From };
2729
}
2830

2931
[Theory]
3032
[MemberData(nameof(Examples))]
31-
public void Parse(string subdivideStr, string fileName, string sheetName, bool needSubdivide, int cutPrefix, int cutPostfix, string keyColumnName, OnOperation oonOperation) {
33+
public void Parse(string subdivideStr, string fileName, string sheetName, bool needSubdivide, int cutPrefix, int cutPostfix, string keyColumnName, int? columnNamesRow, int? dataStartRow, OnOperation oonOperation) {
3234
var subdivide = SheetNameWithSubdivide.FromMixed(subdivideStr);
3335
Assert.True(subdivide.FileName.IsMatch(fileName));
3436
Assert.True(subdivide.SheetName.IsMatch(sheetName));
3537
Assert.Equal(subdivide.NeedSubdivide, needSubdivide);
3638
Assert.Equal(subdivide.CutPrefix, cutPrefix);
3739
Assert.Equal(subdivide.CutPostfix, cutPostfix);
3840
Assert.Equal(subdivide.KeyColumnName, keyColumnName);
41+
Assert.Equal(subdivide.ColumnNamesRow, columnNamesRow);
42+
Assert.Equal(subdivide.DataStartRow, dataStartRow);
3943
Assert.Equal(subdivide.OnOperation, oonOperation);
4044
}
4145
}

seedtable/SeedTableInterface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static DateTime ExcelToSeedCore(IExcelData excelData, string file, FromOptions o
185185
}
186186

187187
static SeedTableBase GetSeedTable(IExcelData excelData, string sheetName, CommonOptions options, SheetNameWithSubdivide subdivide) {
188-
var seedTable = excelData.GetSeedTable(sheetName, options.columnNamesRow, options.dataStartRow, options.ignoreColumns, subdivide.KeyColumnName, options.versionColumn);
188+
var seedTable = excelData.GetSeedTable(sheetName, subdivide.ColumnNamesRow ?? options.columnNamesRow, subdivide.DataStartRow ?? options.dataStartRow, options.ignoreColumns, subdivide.KeyColumnName, options.versionColumn);
189189
if (seedTable.Errors.Count != 0) {
190190
var skipExceptions = seedTable.Errors.Where(error => error is NoIdColumnException);
191191
if (skipExceptions.Count() != 0) {

seedtable/SheetNameWithSubdivide.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@ public static SheetNameWithSubdivide FromMixed(string mixedName) {
1414
var options = result.Groups[5].Value == null ? new string[] { } : result.Groups[5].Value.Split('@');
1515
var onOperation = OnOperation.From | OnOperation.To;
1616
var keyColumnName = "id";
17+
int? columnNamesRow = null;
18+
int? dataStartRow = null;
1719
foreach (var option in options) {
1820
if (Regex.IsMatch(option, $"^(?:from|to)$", RegexOptions.IgnoreCase)) {
1921
Enum.TryParse(option, true, out onOperation);
2022
} else if (option.StartsWith("key=")) {
2123
keyColumnName = option.Substring(4);
24+
} else if (option.StartsWith("column-names-row=")) {
25+
columnNamesRow = int.Parse(option.Substring(17));
26+
} else if (option.StartsWith("data-start-row=")) {
27+
dataStartRow = int.Parse(option.Substring(15));
2228
}
2329
}
2430
var needSubdivide = cutPrefixStr.Length != 0 || cutPostfixStr.Length != 0;
2531
var cutPrefix = cutPrefixStr.Length == 0 ? 0 : Convert.ToInt32(cutPrefixStr);
2632
var cutPostfix = cutPostfixStr.Length == 0 ? 0 : Convert.ToInt32(cutPostfixStr);
27-
return new SheetNameWithSubdivide(fileName, sheetName, needSubdivide, cutPrefix, cutPostfix, keyColumnName, onOperation);
33+
return new SheetNameWithSubdivide(fileName, sheetName, needSubdivide, cutPrefix, cutPostfix, keyColumnName, columnNamesRow, dataStartRow, onOperation);
2834
}
2935

3036
public Wildcard FileName { get; } = null;
@@ -33,6 +39,8 @@ public static SheetNameWithSubdivide FromMixed(string mixedName) {
3339
public int CutPrefix { get; }
3440
public int CutPostfix { get; }
3541
public string KeyColumnName { get; }
42+
public int? ColumnNamesRow { get; }
43+
public int? DataStartRow { get; }
3644
public OnOperation OnOperation { get; }
3745

3846
public SheetNameWithSubdivide(
@@ -42,6 +50,8 @@ public SheetNameWithSubdivide(
4250
int cutPrefix = 0,
4351
int cutPostfix = 0,
4452
string keyColumnName = "id",
53+
int? columnNamesRow = null,
54+
int? dataStartRow = null,
4555
OnOperation onOperation = OnOperation.From | OnOperation.To
4656
) {
4757
FileName = new Wildcard(fileName);
@@ -50,6 +60,8 @@ public SheetNameWithSubdivide(
5060
CutPrefix = cutPrefix;
5161
CutPostfix = cutPostfix;
5262
KeyColumnName = keyColumnName;
63+
ColumnNamesRow = columnNamesRow;
64+
DataStartRow = dataStartRow;
5365
OnOperation = onOperation;
5466
}
5567

0 commit comments

Comments
 (0)