Jake Shuman
Jake Shuman

Reputation: 51

Indent rows with Smartsheet API with Java

I am trying to write a simple program in Java using the Smartsheet API that goes through a fairly large sheet, and indents certain rows. Here is some code similar to what I'm using.

Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken("[My Access Token]").build();
sheetId = 00000000000000; // My Sheet ID
Sheet sheet = smartsheet.sheetResources().getSheet(sheetId, null, null, null, null, null, null, null);
List<Row> rows = sheet.getRows();
Row row = new Row();
row.setId(rows.get(2).getId()); // Updating the second row of the sheet.
row.setIndent(1);
row.setParentId(rows.get(1).getId()); // Set the parent as the row immediately above (which is not indented).
Cell cell = new Cell();
cell.setColumnId(rows.get(1).getCells().get(0).getColumnId());
cell.setValue("Test");
List<Cell> cells = Arrays.asList(cell);
row.setCells(cells);
rows = Arrays.asList(row);
smartsheet.sheetResources().rowResources().updateRows(sheetId, rows);

When I run this, I always get the following error on the last line.

Exception in thread "main" com.smartsheet.api.InvalidRequestException: Invalid row location.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.smartsheet.api.internal.AbstractResources$ErrorCode.getException(AbstractResources.java:147)
at com.smartsheet.api.internal.AbstractResources.handleError(AbstractResources.java:894)
at com.smartsheet.api.internal.AbstractResources.putAndReceiveList(AbstractResources.java:745)
at com.smartsheet.api.internal.SheetRowResourcesImpl.updateRows(SheetRowResourcesImpl.java:252)
at Test3.main(Test3.java:67)

The indentation seems to be causing this, as if I remove the setIndent(...) line, it runs fine. Am I doing something wrong here? Thank you in advance for your help.

Upvotes: 2

Views: 1067

Answers (4)

stmcallister
stmcallister

Reputation: 1719

To add to what Kim said, you can do the indent either by setting the parentId OR by setting the indent. You can't do both or you will get the Invalid row location error you are seeing.

For instance, you can create an instance of your row object and set its id.

    // Row to indent
    Row rowToIndent = new Row();
    rowToIndent.setId(rows.get(2).getId());

Going the parentId route, you need to also set the location. For example, you can setToBottom(true), which will make the row the bottom child row to the parent row. Like this:

    // Set ParentId
    rowToIndent.setParentId(rows.get(1).getId()); // Set the parent as the row immediately above (which is not indented).
    rowToIndent.setToBottom(true);

Or, you can go the sentIndent(1) route with this line:

    // Set Indent
    rowToIndent.setIndent(1);

Also, at this point in time, if you try to indent a row that is already indented or to an ident level that is not possible, you will also receive the Invalid row location exception.

Upvotes: 0

Kim Brandl
Kim Brandl

Reputation: 13500

Remove this line from your code and it should work:

row.setParentId(rows.get(1).getId());

Since you're wanting to update the indent of an existing row, you don't need to specify parentId (the id of the parent row isn't changing).

I've confirmed that the following Update Row request works (successfully indents the specified row and updates the cell value -- just as you're trying to do):

PUT https://api.smartsheet.com/2.0/sheets/8074385778075524/rows
[
    {
        "id": "6033604149045124", 
        "cells": [
            {"columnId": "5004885470013316","value": "TEST_VALUE"}
        ],
        "indent": 1
    }
]

And the following Update Row request fails with the same error that you're reporting (invalid row location), because it includes the parentId property.

[
    {
        "id": "6033604149045124", 
        "cells": [
            {"columnId": "5004885470013316","value": "TEST_VALUE"}
        ],
        "indent": 1,
        "parentId": "2655904428517252"
    }
]

Upvotes: 1

timwells
timwells

Reputation: 341

I did not try your specific code, however, my understanding is that you should be able to setIndent without setting the parentId. Use parentId when you are adding new rows, setIndent when you are updating existing rows.

Upvotes: 0

IspellColourWithAU
IspellColourWithAU

Reputation: 63

I think it's trying to access a row that doesn't exist.

Upvotes: 0

Related Questions