Skip to content

Commit 8ca1b19

Browse files
committed
missing files
1 parent 562f6a5 commit 8ca1b19

File tree

2 files changed

+119
-5
lines changed

2 files changed

+119
-5
lines changed

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
*.class
22

3-
# Mobile Tools for Java (J2ME)
4-
.mtj.tmp/
5-
63
# Package Files #
74
*.jar
85
*.war
96
*.ear
107
workspace.xml
118
target/*
129

13-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1410
hs_err_pid*
1511
lib/
1612
target/
1713
.idea/libraries/
1814
.project
1915
.settings/
2016
bin/
21-
src/main/
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.smartsheet.samples;
2+
3+
// Add Maven library "com.smartsheet:smartsheet-sdk-java:2.2.3" to access Smartsheet Java SDK
4+
import com.smartsheet.api.Smartsheet;
5+
import com.smartsheet.api.SmartsheetBuilder;
6+
import com.smartsheet.api.models.Cell;
7+
import com.smartsheet.api.models.Column;
8+
import com.smartsheet.api.models.Row;
9+
import com.smartsheet.api.models.Sheet;
10+
11+
import java.io.FileInputStream;
12+
import java.util.*;
13+
14+
15+
public class RWSheet {
16+
static {
17+
// These lines enable logging to the console
18+
System.setProperty("Smartsheet.trace.parts", "RequestBodySummary,ResponseBodySummary");
19+
System.setProperty("Smartsheet.trace.pretty", "true");
20+
}
21+
22+
// The API identifies columns by Id, but it's more convenient to refer to column names
23+
private static HashMap<String, Long> columnMap = new HashMap<String, Long>(); // Map from friendly column name to column Id
24+
25+
public static void main(final String[] args) {
26+
27+
try {
28+
// Get API access token from properties file or environment
29+
Properties prop = new Properties();
30+
prop.load(new FileInputStream("RWSheet.properties"));
31+
32+
String accessToken = prop.getProperty("accessToken");
33+
if (accessToken == null || accessToken.isEmpty())
34+
accessToken = System.getenv("SMARTSHEET_ACCESS_TOKEN");
35+
if (accessToken == null || accessToken.isEmpty())
36+
throw new Exception("Must set API access token in rwsheet.properties file");
37+
38+
// Get sheetId from properties file
39+
String sheetIdString = prop.getProperty("sheetId");
40+
Long sheetId = Long.parseLong(sheetIdString);
41+
42+
// Initialize client
43+
Smartsheet ss = new SmartsheetBuilder().setAccessToken(accessToken).build();
44+
45+
// Load the entire sheet
46+
Sheet sheet = ss.sheetResources().getSheet(sheetId, null, null, null, null, null, null, null);
47+
System.out.println("Loaded " + sheet.getRows().size() + " rows from sheet: " + sheet.getName());
48+
49+
// Build the column map for later reference
50+
for (Column column : sheet.getColumns())
51+
columnMap.put(column.getTitle(), column.getId());
52+
53+
// Accumulate rows needing update here
54+
ArrayList<Row> rowsToUpdate = new ArrayList<Row>();
55+
56+
for (Row row : sheet.getRows()) {
57+
Row rowToUpdate = evaluateRowAndBuildUpdates(row);
58+
if (rowToUpdate != null)
59+
rowsToUpdate.add(rowToUpdate);
60+
}
61+
62+
if (rowsToUpdate.isEmpty()) {
63+
System.out.println("No updates required");
64+
} else {
65+
// Finally, write all updated cells back to Smartsheet
66+
System.out.println("Writing " + rowsToUpdate.size() + " rows back to sheet id " + sheet.getId());
67+
ss.sheetResources().rowResources().updateRows(sheetId, rowsToUpdate);
68+
System.out.println("Done");
69+
}
70+
} catch (Exception ex) {
71+
System.out.println("Exception : " + ex.getMessage());
72+
ex.printStackTrace();
73+
}
74+
}
75+
76+
/*
77+
* TODO: Replace the body of this loop with your code
78+
* This *example* looks for rows with a "Status" column marked "Complete" and sets the "Remaining" column to zero
79+
*
80+
* Return a new Row with updated cell values, else null to leave unchanged
81+
*/
82+
private static Row evaluateRowAndBuildUpdates(Row sourceRow) {
83+
Row rowToUpdate = null;
84+
85+
// Find cell we want to examine
86+
Cell statusCell = getCellByColumnName(sourceRow, "Status");
87+
88+
if ("Complete".equals(statusCell.getDisplayValue())) {
89+
Cell remainingCell = getCellByColumnName(sourceRow, "Remaining");
90+
if (! "0".equals(remainingCell.getDisplayValue())) // Skip if "Remaining" is already zero
91+
{
92+
System.out.println("Need to update row #" + sourceRow.getRowNumber());
93+
94+
Cell cellToUpdate = new Cell();
95+
cellToUpdate.setColumnId(columnMap.get("Remaining"));
96+
cellToUpdate.setValue(0);
97+
98+
List<Cell> cellsToUpdate = Arrays.asList(cellToUpdate);
99+
100+
rowToUpdate = new Row();
101+
rowToUpdate.setId(sourceRow.getId());
102+
rowToUpdate.setCells(cellsToUpdate);
103+
}
104+
}
105+
return rowToUpdate;
106+
}
107+
108+
// Helper function to find cell in a row
109+
static Cell getCellByColumnName(Row row, String columnName) {
110+
Long colId = columnMap.get(columnName);
111+
112+
return row.getCells().stream()
113+
.filter(cell -> colId.equals((Long) cell.getColumnId()))
114+
.findFirst()
115+
.orElse(null);
116+
117+
}
118+
119+
}

0 commit comments

Comments
 (0)