Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear all RelId's if workbook is loaded from template. #1172

Merged
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -110,7 +110,7 @@ public static IXLOutline DefaultOutline

public static XLWorkbook OpenFromTemplate(String path)
{
return new XLWorkbook(path, true);
return new XLWorkbook(path, asTemplate: true);
}

#endregion Static
@@ -52,6 +52,33 @@ private void LoadSheetsFromTemplate(String fileName)
{
using (var dSpreadsheet = SpreadsheetDocument.CreateFromTemplate(fileName))
LoadSpreadsheetDocument(dSpreadsheet);

// If we load a workbook as a template, we have to treat it as a "new" workbook.
// The original file will NOT be copied into place before changes are applied
// Hence all loaded RelIds have to be cleared
ResetAllRelIds();
}

private void ResetAllRelIds()
{
foreach (var ws in Worksheets.Cast<XLWorksheet>())
{
ws.SheetId = 0;
ws.RelId = null;

foreach (var pt in ws.PivotTables.Cast<XLPivotTable>())
{
pt.WorkbookCacheRelId = null;
pt.CacheDefinitionRelId = null;
pt.RelId = null;
}

foreach (var picture in ws.Pictures.Cast<XLPicture>())
picture.RelId = null;

foreach (var table in ws.Tables.Cast<XLTable>())
table.RelId = null;
}
}

private void LoadSpreadsheetDocument(SpreadsheetDocument dSpreadsheet)
@@ -72,10 +72,17 @@ public static void RunTestExample<T>(string filePartName, bool evaluateFormulae

filePath1 = Path.Combine(directory, "z" + fileName);
var filePath2 = Path.Combine(directory, fileName);

//Run test
example.Create(filePath1);
using (var wb = new XLWorkbook(filePath1))
wb.SaveAs(filePath2, true, evaluateFormulae);
wb.SaveAs(filePath2, validate: true, evaluateFormulae);

// Also load from template and save it again - but not necessary to test against reference file
// We're just testing that it can save.
using (var ms = new MemoryStream())
using (var wb = XLWorkbook.OpenFromTemplate(filePath1))
wb.SaveAs(ms, validate: true, evaluateFormulae);

if (CompareWithResources)
{
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.