Posted 17 May 2021, 9:09 am EST
Hello,
I replied to a previous post earlier but it was already marked as answered so I thought I should raise the same issue on a new post
Forums Home / Spread / SpreadJS
Posted by: tadekunle on 17 May 2021, 9:09 am EST
Posted 17 May 2021, 9:09 am EST
Hello,
I replied to a previous post earlier but it was already marked as answered so I thought I should raise the same issue on a new post
Posted 17 May 2021, 9:27 am EST
Hi,
For this you need to also merge the names json to the target workbookJSON, Please refer to the following code snippet and attached sample that demonstrates the same.
let newSheetName = getUniqueName(key, "", sheetNamesSet);
if (wbJSON.names.length > 0) {
wbJSON.names.forEach((name) => {
if (name.formula.startsWith(sheetJson.name)) {
name.formula = name.formula.replaceAll(
sheetJson.name,
newSheetName
);
}
});
targetWbJSON.names = targetWbJSON.names.concat(wbJSON.names);
sample: https://codesandbox.io/s/spread-js-starter-forked-260gw?file=/src/index.js:1708-1733
Regards,
Avinash
Posted 18 May 2021, 5:58 pm EST
Hi Avinash,
Thank you for your reply. The snippet you provided worked for me. However, I am trying to ensure that when I import the same excel file again, it does not create a new sheet with a unique name, but it replaces the previous sheet created as well as its named cells. So I want to import and merge a single excel file as many times as possible, and overwrite the named cells available on the spread with the name cells in the excel file I just imported.
Thank you,
Tolu
Posted 19 May 2021, 6:49 am EST
Hi Tolu,
For this, you first need to decide when two-sheets are said to be equal and according to that, you just need to replace the old sheet json with newSheet Json.
Please refer to the following code snippet and attached sample which demonstrates how you replace the sheet if the name of the sheet is the same(please note that this condition could be anything such data on a sheet or some styles. you need to check it according your requirement and replace only if it duplicate)
// Replace the sheet;
sheetNamesSet.add(sheetJson.name);
targetWbJSON.sheets[sheetJson.name] = sheetJson;
targetWbJSON.sheetCount = sheetNamesSet.size;
targetWbJSON.names = targetWbJSON.names || [];
targetWbJSON.names = targetWbJSON.names.concat(wbJSON.names);
sample: https://codesandbox.io/s/spread-js-starter-forked-rejkm?file=/src/workbookJsonMergeHelper.js
Regards
Avinash