Update: Smartsheet does now support adding or updating formulas via the API which can be found in the documentation for adding a row and updating a row.
The main difference is to set the formula
in the row object instead of setting the value
.
Original Answer
You are correct, formulas are not currently supported via the API. Although, we do plan to add this functionality in the future.
Currently, if you try to send a formula to the API it will be handled as a string and a single quote will be added to the beginning of the formula. Then the only way to convert the string back to a formula is to manually remove the single quote when inside the Smartsheet UI.
Available Option
Your suggestion to use a template will definitely work if you will always be using the same formulas. The process will look like the following:
- Setup a template with the formulas that you want to use.
- Create a new sheet from the template.
- Add extra data to the new sheet that the formulas will use.
Note: rows that have never been used cannot be updated since they do not exist. So, in the template you can initialize the rows by putting a word in the locations that you want to update. For example, you could put the word "PLACEHOLDER" in all of the locations that you intend to update.
I have added two examples below one using curl and the other using our Java SDK.
Curl Example
Create a new sheet from the Template. Make sure to replace YOUR_TOKEN
and YOUR_TEMPLATE_OR_SHEET_ID
in the below command.
curl https://api.smartsheet.com/1.1/sheets?include=data,attachments,discussions -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -X POST -d '{"name":"newSheetFromTemplate","fromId":"YOUR_TEMPLATE_OR_SHEET_ID"}'
Then take the sheet id from the response and issue a command to get the row id's.
curl https://api.smartsheet.com/1.1/sheet/YOUR_SHEET_ID -H "Authorization: Bearer YOUR_TOKEN"
Last, grab the row id and column id from the response and issue a command to update the appropriate cells. I'm updating two cells with the values 1 and 2 with the below command.
curl https://api.smartsheet.com/1.1/row/YOUR_ROW_ID/cells -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -X PUT -d '[ {"columnId": YOUR_COLUMN_ID, "value": 1}]'
curl https://api.smartsheet.com/1.1/row/YOUR_ROW_ID/cells -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -X PUT -d '[ {"columnId": YOUR_COLUMN_ID, "value": 2}]'
SDK Example
This example requires installing our Java SDK. There is also a C# SDK that works in a very similar fashion.
import java.util.EnumSet;
import java.util.List;
import com.smartsheet.api.Smartsheet;
import com.smartsheet.api.SmartsheetBuilder;
import com.smartsheet.api.models.Cell;
import com.smartsheet.api.models.Column;
import com.smartsheet.api.models.ObjectInclusion;
import com.smartsheet.api.models.Row;
import com.smartsheet.api.models.Sheet;
import com.smartsheet.api.SmartsheetException;
public class Test {
public static void main(String[] args) throws SmartsheetException {
// Setup a Smartsheet object
Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken("YOUR_TOKEN").build();
// Create a copy of a sheet from the template
Sheet newSheet = new Sheet.CreateFromTemplateOrSheetBuilder().setFromId(YOUR_TEMPLATE_OR_SHEET_ID).setName("newSheetName").build();
newSheet = smartsheet.sheets().createSheetFromExisting(newSheet, EnumSet.allOf(ObjectInclusion.class));
// Get the columns/rows/data for the sheet we just created
newSheet = smartsheet.sheets().getSheet(newSheet.getId(), EnumSet.allOf(ObjectInclusion.class));
// Grab the column and rows that will be updated in the new sheet
Column column1 = newSheet.getColumnByIndex(0);
Row row1 = newSheet.getRowByRowNumber(1);
Row row2 = newSheet.getRowByRowNumber(2);
// Setup two cells for the the specified columns
List<Cell> newCell1 = new Cell.UpdateRowCellsBuilder().addCell(column1.getId(), 1).build();
List<Cell> newCell2 = new Cell.UpdateRowCellsBuilder().addCell(column1.getId(), 2).build();
// Update the cell for the specified row
smartsheet.rows().updateCells(row1.getId(), newCell1);
smartsheet.rows().updateCells(row2.getId(), newCell2);
}
}