Trying simple dataset add, having script probelms

So one window, one text input, one drop down, and a timestamp. here is my code below:

ds = system.tag.read("[default]MyData").value

numbers = event.source.parent.getComponent('Text Field').text
dropselect = event.source.parent.getComponent('Dropdown').selectedLabel

newVal = ["numbers", "dropselect"]

system.tag.write("[default]MyData", system.dataset.setValue(ds, row, col, newVal))

# below is alternate ending, neither worked.
ds.data = system.dataset.addRow(ds.data, 0, newVal)

thanks in advance

dataset has one extra column for timestamp.

Why are you trying to put a list (two values) into one specific cell of a dataset?

Some general tips when trouble shooting and asking for help on forums:

  • reduce the example to the minimum. Don't introduce things that have nothing to do with the problem you're trying to solve: text fields, dropdowns... etc. Especially when you're not using them (numbers and dropselect)
  • include the whole thing. You're using variables that are not declared in the code you posted (row and col)
  • deconstruct the process and check things at every step.
  • explain what you actually want to do. All we have to figure it out here is your code, which is not very descriptive, and if it's wrong then there's no way for us to really understand what it's supposed to do.
    The title says you want to add things, but nothing in the code actually adds anything.

Thought that was what I needed to do reading on the forum.

i just have a text input box, drop down, and submit button.

when i submit I want a new row to be created with time stamp , text, drop down selection.

then I want to view that dataset in a power table.

I am using numbers and dropselect though?

I thought row and col were generic? if I set row number will it add or overwrite?

Then you'll need addRow.
Note that you'll probably need to supply a value for every column, which is why your attempt didn't work.

No you're not. Your newVal variable is a list with the literal strings "numbers" and "dropselect", not the values from the variables.
row and col have no special meaning.

Can I supply a value for every col in the statement? or do I need a separate add for every col? couldn't find an example of multiple in one statement.

Your addRow attempt might also have failed because of that .data. Is your MyData tag just a dataset tag ?

Just one call is enough. The function does what its name says: It adds a row.

What you're looking for is system.dataset.addRow()

#get the original dataset value
ds = system.tag.readBlocking(['[default]MyData'])[0].value

#get the values from the components to insert in the dataset
#You will need to wrap them in a list.

numbers = event.source.parent.getComponent('Text Field').text
dropselect = event.source.parent.getComponent('Dropdown').selectedLabel

newRow = [numbers,dropselect]

#add row to dataset and write to the tag
system.tag.writeBlocking(['[default]MyData'],[system.dataset.addRow(ds,row=newRow)])

its a dataset memory tag

Do i just newval1,newval2,newval3 ?

Here's an example that adds a row to a dataset:

ds = system.dataset.toDataSet(
	['name', 'value', 'something'],
	[
		['foo', 21, 'a'],
		['bar', 12, 'b']
	]
)

ds = system.dataset.addRow(ds, ['pox', 42, 'c'])

Maybe it will help make things clearer ?

I got

Traceback (most recent call last):
  File "<event:actionPerformed>", line 13, in <module>
TypeError: addRow(): takes no keyword arguments

just remove the row=

May need to include 0 for the index.

remove the row= bit:
system.dataset.addRow(ds, newRow)]

But you'll also need a value for the timestamp column

nope it's optional. The function will append the new row to the end of the dataset if the row index is not provided.
If you want the row inserted at the top or anywhere else, then indeed you'll need to provide the index.

Yeah, I know it's optional, but "generally", if an optional parameter is skipped then you need to provide the keyword argument which is why I included it. Kind of weird that it doesn't accept keyword arguments, that seems to go against the standard.

timestamp = "[System]Client/System/CurrentDateTime.Value"

newRow = [numbers,dropselect,timestamp]

i get cannot coerce error on timestamp

Guess I missed the timestamp need as well.

Here is corrected code:

#get the original dataset value
ds = system.tag.readBlocking(['[default]MyData'])[0].value

#get the values from the components to insert in the dataset
#You will need to wrap them in a list.

numbers = event.source.parent.getComponent('Text Field').text
dropselect = event.source.parent.getComponent('Dropdown').selectedLabel

newRow = [numbers,dropselect,system.date.now()]

#add row to dataset and write to the tag
system.tag.writeBlocking(['[default]MyData'],[system.dataset.addRow(ds,newRow)])

getting

Traceback (most recent call last):
  File "<event:actionPerformed>", line 13, in <module>
ValueError: Cannot coerce value '<java function now 0x2>' into type: class java.util.Date



Because that's a string, not a timestamp.

If you want to get the value from a tag, you'll need to use one of the tag read functions:

tstamp = system.tag.readBlocking(["[System]Client/System/CurrentDateTime.Value"])[0].value

or maybe use system.date.now to get the current time.

1 Like

You grabbed it before I corrected the () I left off of the now() function. Try again.

And also remove the comma at the end of the list, if you didn't already. That's what I get for going fast. uggh