setValue() to create row if rowindex is outside current set

Posted by: datta on 28 May 2019, 5:12 am EST

  • Posted 28 May 2019, 5:12 am EST

    Hi,

    
    spread.addSheet(0, sheet);
    sheet.setRowCount(3,GC.Spread.Sheets.SheetArea.viewport);
     sheet.setValue(4, 0, 123);
    

    This code wont work because I am setting value on row 4 which doesn’t exist now. To make it work correctly I need add a check (rowIndex >= sheet.getRowCount() -1) and need to invoke addRows(). This becomes bit ugly. Is there any better way to do this?

  • Posted 29 May 2019, 2:10 am EST

    Hi,

    As you have mentioned, the row/column count check is necessary if you need to increase the row count in case the row doesn’t exist. If you don’t want to do this multiple time, then you could just create a helper function to do it for you. Please refer to the following code snippet:

    function setValueModified(sheet, rowIndex, colIndex, value){
    	/* add rows if it doesn't exist	*/
    	if(sheet.getRowCount() <= rowIndex){
    		sheet.setRowCount(rowIndex + 1);
    	}
    	/* add columns if doesn't exists */
    	if(sheet.getColumnCount() <= colIndex){
    		sheet.setColumnCount(colIndex + 1);
    	}
    	/* set value */
    	sheet.setValue(rowIndex, colIndex, value);
    }
    /* set value */
    setValueModified(sheet, 8, 9, "value");
    

    Regards

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels