andmarti1424 / sc-im

sc-im - Spreadsheet Calculator Improvised -- An ncurses spreadsheet program for terminal
Other
4.8k stars 203 forks source link

Fix issue #784, exposes row and column of calling cell in @lua script #814

Closed gzagatti closed 1 year ago

gzagatti commented 1 year ago

I'm really excited about this project. I was looking for a terminal spreadsheet that could help keep track of experiment results and build Latex table.

As I started playing around with sc-im I noticed that the script called by @lua did not have access to the reference of the calling cell which limits the flexibility of this function (see Issue #784). On top of that, it did not return string values like other string functions. This PR solves both issues.

More specifically, I have modified the doLUA function. It injects the row and column numbers of the calling cell in the "luascript". Injecting global variables is not the cleanest approach.

It would be better to allow @lua a third parameter that specify which function should be called from the script. However because it would be a breaking change, I have left it for discussion here. It could be the case of defining an additional function e.g. @lua_pcall. However, @lua is shorter.

I hope the screenshot below gives you a better idea.

Screenshot from 2023-05-08 20-26-44

I am new to this project. Please let me know if there's anything I missed. I am open to suggestions.

andmarti1424 commented 1 year ago

@gzagatti Thank you for your work. Will take a look at it.

roman65536 commented 1 year ago

@gzagatti

Basically I like your approach. If you could even pass to lua from which part it is called (eval or seval), user can solve the "twice" lua calling delimma in the lua script and distinguish whether it was called from the eval() part of dolookup() or seval() part. Since you are checking, whether the return type is "string", you could also check for "Number" and set the Cell value accordingly... How about it ??

Btw: just as an fun fact, since you are writing about "keep track of experiment results and build Latex table" ... I'm using Lualatex to measure all kind of electronic stuff using measurement equipment (power supply, generator, oscilloscope, meter etc) and generate all tables, graphics result etc right using lualatex script ;)

rgds Roman

gzagatti commented 1 year ago

Thanks @roman65536 for your feedback.

It's totally feasible to add @lua to eval. This is something I considered for this PR, but I was not sure whether to implement. I thought that the user my be able to set the numeric value of the cell from the script simply by sc.lsetnum(r, c).

I'm not yet very familiar with sc-im. Could you elaborate the "twice" lua calling dilemma? And what's dolookup()? Is it similar to vlookup in Excel? It's not listed in the manual as a cell function.

Let me try to incorporate your suggestions.

I also use lualatex to compile my papers. But nothing like generating tables using lualatex script. At the moment, I'm running a bunch of ad-hoc experiments and need a sane way of keeping all results in one place. LibreOffice is too much for the task, and markdown too basic.

andmarti1424 commented 1 year ago

@gzagatti thanks. Merged to dev branch!

gzagatti commented 1 year ago

You're welcome!