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

Error: Shared Formula master must exist above and or left of clone #676

Open
robophil opened this issue Oct 28, 2018 · 9 comments
Open

Error: Shared Formula master must exist above and or left of clone #676

robophil opened this issue Oct 28, 2018 · 9 comments
Labels

Comments

@robophil
Copy link

robophil commented Oct 28, 2018

I want to insert new rows dynamically into my sheet and have them inherit the formula used above.

  const workbook = await readWorkbook('./sample-sheet.xlsx')
  const worksheet = workbook.getWorksheet('Consumables')
  const row = worksheet.getRow(7)

  const column = 'O'
  const value = row.getCell(column).value
  console.log(`current value of ${column}`)
  console.log(value)
  console.log(row.getCell(column).formulaType)

  row.getCell(column).value = {
    sharedFormula: `${column}6`, result: 0
  }

  row.commit()

  await saveWorkbook(workbook, 'export.xlsx')

It logs this error

Error: Shared Formula master must exist above and or left of clone
 .....
......

This works for some columns. So my question is, how do I set a share formula? or better yet, how can I achieve this differently if there's an alternative

@williamluke4
Copy link

@robophil Did you manage to solve this?

@Siemienik Siemienik added the bug label Jan 11, 2019
@enzoferey
Copy link

So this was happening to me as well and I eventually found a solution that works well: just copy the whole value.

For example:

// Master
const masterCell = worksheet.getCell("A3")
masterCell.value = {
    formula: "A1+A2",
    result: myResult
}

// anywhere later...

// "Shared"
worksheet.getCell("A4").value = masterCell.value;

Now if you change the values or A1 or A2, both A3 and A4 will get updated. If you open the .xlsx export you will see that both cells use the same formula.

@robophil
Copy link
Author

@robophil Did you manage to solve this?

I didn't actually. My previous company opted for https://github.com/SheetJS/js-xlsx with pro support.

@joobisb
Copy link

joobisb commented Apr 13, 2020

@robophil @enzoferey @williamluke4 i'm still facing this issue any solutions for this?..i have an excel with lot of formulas but i'm not changing anything just added a new column using spliceColumns and inserted few values in that column..these column doesnt have any formulas, but I'm getting the error mentioned here..

@taviroquai
Copy link

I'm also facing this problem. I wish the error at least tells me which cell? Thanks!

@joobisb
Copy link

joobisb commented May 6, 2020

@taviroquai
You can see the cell number in logs, they have added cell as part of error log in a commit 2 months ago. I think if you pull the latest version you can get it.
my issue was i was inserting a column in between another 2 columns and as a result the formula which were already existing on the sheet get dispersed.

@taviroquai
Copy link

@joobisb thanks for the info. I left original cells with formula intact and only fill new values/formula on empty cells. It was the only way to get around this.

@FraeuleinWild
Copy link

I finally realized what causes the error and it is really simple to overcome. Apparently dragging and dropping formulas in the excel template create these "slave-master relationships" that the library can't handle. The error does not occur if the templates formulas are inserted manually. So instead of writing in Cell A11: "=sum(A1:A10)" and then dragging the formula to cell B11 it is important to manually type in B11 "=sum(B1:B10)". The formulas generated by dragging and dropping are completely the same, but treated differently internally.

@davidmricardo
Copy link

davidmricardo commented Sep 28, 2022

If in the final excel you only care about the values you can test if it is a shared formula and grab the result:

row[22] = row[22]['sharedFormula'] ? row[22]['result'] : row[22];

Or you follow the link:

if (row[22]['sharedFormula']) {
var idx = row[22]['sharedFormula'].substr(1); // assuming it points to the same column wich is my case
row[22] = rows[idx][22];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants