Search code examples
numbersjavascript-automationiwork

I can't get Delete Row/Remove to work in Mac Numbers with JXA


I have written a JXA script for Numbers that coverts a standard eCommerce order spreadsheet to the Stamps.com format for uploading the order, to print shipping labels. After 2 weeks of digging for info on JXA for Numbers, I have the conversion at 90%. But, I need to delete some non-relevant rows and can't get that function to work. I am also confused between .deleteRow() and .remove(). The commented-out code was a different attempt which also did not work. However, using the table.ranges["6:6"] I did manage to select (highlight) the entire row 6, but then no delete. Here is my current code (fragment).


var Numbers = Application('Numbers')
Numbers.includeStandardAdditions = true;

const myFilePath = "/Users/jds/Sites/Customer Excel sheets for STAMPS/";

// Point to the file and open it 
var path = Numbers.chooseFile({ withPrompt: 'Please select spreadsheet file (.xls, .xlsx, .numbers' })  

var doc = Numbers.open(path) 

// Access the first table of the first sheet of the document 

thesheet = doc.sheets[0]
table = thesheet.tables[0]

...


// Check for "Marci Bee" example (highlighted) row present at row 6; if so, delete the row.

    if (table.cells["E6"].value() === "Marci Bee") {
        Numbers.displayAlert("Marci is here.")    // *** DEBUG *** (This works)
        const marciRow = 6
        thesheet.deleteRow(marciRow);

/*
    Numbers.displayAlert("Ok 1")                        // *** DEBUG ***    
        table.rangeSelection = table.ranges["6:6"]
    Numbers.displayAlert("Ok 2")                        // *** DEBUG ***
        rmMyRow = table.selectionRange()        
    Numbers.displayAlert("Ok 3")                        // *** DEBUG ***    
        rmMyRow.delete()
*/

    Numbers.displayAlert("Marci is gone.")              // *** DEBUG ***

    }


Solution

  • table.rows[marciRow-1].delete(); //the index is zero based