Rename Plots Based on a Variable Column Header in Grapher

By default, Grapher names new plots using the value in the first row of the Y variable column. However, this behavior differs in older Grapher versions, and you may have existing files with plots that aren’t named this way. The script below renames plots based on the Y variable column header. It applies to plot types with a YCol property, but you can modify it to work with other plot types or use a different column (e.g., the X variable column) for naming.

 

To run this script:

  1. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer.
  2. Double click on Scripter.exe to launch Scripter.
  3. Copy and paste the script below into Scripter's code window (be sure to delete any existing lines first), or simply download the attached BAS file and open it in Scripter.
  4. Update the inputFile variable under the USER-DEFINED VARIABLES section at the beginning of the script. If you do not update this variable, the script will still run using a Grapher sample file. 
  5. Click Script | Run to run the script.

'===========================================================
'This script renames plots based on the Y variable column 
'header. It applies only to plot types with a YCol property. 
'Refer to the "YCol Property" page in the Help for a list of 
'supported plot types.
'===========================================================
Sub Main
    
    '=======================================================
    ' USER-DEFINED VARIABLES
    '=======================================================
    ' Path to input GRF or GPJ file
    inputFile = "C:\Program Files\Golden Software\Grapher\Samples\Graph Magnifier.gpj"
    '=======================================================

    ' Initialize Grapher
    Dim Grapher As Object
    Set Grapher = CreateObject("Grapher.Application")
    Grapher.Visible = True

    ' Open the input file in a plot document
    Dim PlotDoc As Object
    Set PlotDoc = Grapher.Documents.Open(inputFile)

    ' Initialize objects used in the for loop below
    Dim Graph As Object, Plot As Object, Wks As Object

    ' Loop through all shape objects in the plot document
    For i = 1 To PlotDoc.Shapes.Count
        
        ' If the shape object is graph...
        If PlotDoc.Shapes.Item(i).Type = grfShapeGraph Then
    
            ' Create and object pointer to the graph 
            Set Graph = PlotDoc.Shapes.Item(i)

            ' Loop through the plots under the graph
            For j = 1 To Graph.Plots.Count
                                
                ' Create an object pointer to the plot object 
                Set Plot = Graph.Plots.Item(j)

                ' Get the worksheet used by this plot
                wksPath = Plot.Worksheet
                Set Wks = Grapher.Documents.Open(wksPath)

                ' Get the column index from the plot
                columnIndex = Plot.YCol

                ' Get the column name from the first worksheet row
                colName = Wks.Cells(1, columnIndex)

                ' Use the the column name to rename the plot
                Plot.Name = colName

                ' Close the worksheet
                Wks.Close(grfSaveChangesNo)
            Next
        End If
    Next
End Sub

Updated March 2025

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.