Home » Table Functions » Understanding the table.cell_set_text() Function in Pine Script

Understanding the table.cell_set_text() Function in Pine Script

Photo of author
Published on

The table.cell_set_text() function in Pine Script is a powerful tool for modifying the content of cells within a table created on a TradingView chart. This function allows for dynamic updates to table cells, making it invaluable for displaying real-time information or changing data points based on script logic. Let’s dive into the details of this function, its syntax, arguments, and see an example in action.

Syntax

The syntax of the table.cell_set_text() function is straightforward:

table.cell_set_text(table_id, column, row, text) → void

This function does not return any value (void), indicating its purpose is purely to perform an action rather than calculate and provide data.

Arguments

  • table_id (series table): This argument takes a table object, which is the target table where the text will be set.
  • column (series int): The column index of the cell you want to modify. Remember, column indexing starts at 0.
  • row (series int): The row index of the cell you are targeting for text modification. Like columns, row indexing also starts at 0.
  • text (series string): The new text you wish to display in the specified cell.

Example and Explanation

Consider the following Pine Script example that utilizes the table.cell_set_text() function:

//@version=5
indicator("TABLE example", overlay = true)
var tableId = table.new(position = position.top_left, rows = 1, columns = 2, bgcolor = color.yellow, border_width = 1)
table.cell(tableId, row = 0, column = 0, text = "InitialText", text_color = color.blue)
table.cell_set_text(tableId, row = 0, column = 0, text = "UpdatedText")
Example and Explanation

Walkthrough of code 

  1. Indicator Declaration: First, we declare a custom indicator named “TABLE example” that will be displayed on the chart.
  2. Table Creation: We create a new table tableId with table.new() positioned at the top left of the chart. It consists of 1 row and 2 columns, with a yellow background and a border width of 1.
  3. Initial Cell Setting: Using table.cell(), we initially set the text of the cell at row 0, column 0 to “InitialText” with blue text color.
  4. Text Update with table.cell_set_text: Finally, we update the text of the same cell to “UpdatedText” using table.cell_set_text().

Key Features and Takeaways

  • Function Usability: The table.cell_set_text() function is essential for updating cell content dynamically in Pine Script tables.
  • Syntax and Application: Its syntax is clear and requires specifying the table ID, along with the cell’s row and column indexes, followed by the new text content.
  • Real-time Data Display: This function is particularly useful for scripts that need to display changing data points or indicators dynamically within a table on a TradingView chart.

In conclusion, the table.cell_set_text() function offers a straightforward way to modify the content of table cells in Pine Script, making it a crucial feature for creating interactive and informative indicators on TradingView.

Leave a Comment