package uk.co.metricrat.gsai2; import android.app.*; import android.content.*; import com.google.appinventor.components.annotations.*; import com.google.appinventor.components.runtime.*; import com.google.appinventor.components.runtime.errors.YailRuntimeError; import com.google.appinventor.components.runtime.util.*; import org.json.*; import java.io.*; import java.net.*; public class GSAI2 extends AndroidNonvisibleComponent { private final Activity activity; private final Context context; private String sheetId = ""; private String sheetName = ""; private String scriptUrl = ""; public GSAI2(ComponentContainer container) { super(container.$form()); this.activity = container.$context(); context = container.$context(); } @SimpleFunction(description = "Get A1 Notation reference for a cell") public void GetCellRef(String column, String row) { String params = scriptUrl + "?FN=getCellRef" + "&ID=" + sheetId + "&SH=" + sheetName + "&ROW=" + row + "&COL=" + column; String event = "getCellRef"; GetFunction(params, event); } @SimpleFunction(description = "Get A1 Notation reference for a range") public void GetRangeRef(String column, String row, String column1, String row1) { String params = scriptUrl + "?FN=getRangeRef" + "&ID=" + sheetId + "&SH=" + sheetName + "&ROW=" + row + "&COL=" + column + "&ROW1=" + row1 + "&COL1=" + column1; String event = "getRangeRef"; GetFunction(params, event); } @SimpleFunction(description = "Enter A1 notation reference to return cell value") public void ReadCell(String ref) { String params = scriptUrl + "?FN=readCell" + "&ID=" + sheetId + "&SH=" + sheetName + "&REF=" + ref; String event = "readCell"; GetFunction(params, event); } @SimpleFunction(description = "Enter row number to return all values in row as a stringified JSON array") public void ReadRow(String rowNumber) { String params = scriptUrl + "?FN=readRow" + "&ID=" + sheetId + "&SH=" + sheetName + "&ROW=" + rowNumber; String event = "readRow"; GetFunction(params, event); } @SimpleFunction(description = "Enter column number (A=1/B=2/etc.) to return all values in column as a stringified JSON array") public void ReadColumn(String columnNumber) { String params = scriptUrl + "?FN=readCol" + "&ID=" + sheetId + "&SH=" + sheetName + "&COL=" + columnNumber; String event = "readCol"; GetFunction(params, event); } @SimpleFunction(description = "Enter A1 notation reference to return range values as a stringified JSON array") public void ReadRange(String ref) { String params = scriptUrl + "?FN=readRange" + "&ID=" + sheetId + "&SH=" + sheetName + "&REF=" + ref; String event = "readRange"; GetFunction(params, event); } @SimpleFunction(description = "Returns all values on sheet, including headers, as a stringified JSON array") public void ReadSheet() { String params = scriptUrl + "?FN=readSheet" + "&ID=" + sheetId + "&SH=" + sheetName; String event = "readSheet"; GetFunction(params, event); } @SimpleFunction(description = "Outputs results for SQL query of all data as a stringified JSON array. Use hdr = 1 to return headers, else hdr = 0 ") public void QuerySheet(String hdr, String sql) { String params = scriptUrl + "?FN=querySheet" + "&ID=" + sheetId + "&SH=" + sheetName + "&HDR=" + hdr + "&SQL=" + sql; String event = "querySheet"; GetFunction(params, event); } @SimpleFunction(description = "Enter row number to delete that row") public void DeleteRow(String rowNumber) { String params = scriptUrl + "?FN=deleteRow" + "&ID=" + sheetId + "&SH=" + sheetName + "&ROW=" + rowNumber; String event = "deleteRow"; GetFunction(params, event); } @SimpleFunction(description = "Enter column number to delete that column") public void DeleteColumn(String columnNumber) { String params = scriptUrl + "?FN=deleteCol" + "&ID=" + sheetId + "&SH=" + sheetName + "&COL=" + columnNumber; String event = "deleteCol"; GetFunction(params, event); } @SimpleFunction(description = "Enter A1 notation reference and value to write cell value") public void WriteCell(String ref, String data) { String params = scriptUrl + "?FN=writeCell" + "&ID=" + sheetId + "&SH=" + sheetName + "&REF=" + ref + "&DATA=" + data; String event = "writeCell"; GetFunction(params, event); } @SimpleFunction(description = "Enter row number and values to write row values") public void WriteRow(String rowNumber, String data) { String params = scriptUrl + "?FN=writeRow" + "&ID=" + sheetId + "&SH=" + sheetName + "&ROW=" + rowNumber + "&DATA=" + data; String event = "writeRow"; GetFunction(params, event); } @SimpleFunction(description = "Enter column number and values to write column values") public void WriteColumn(String columnNumber, String data) { String params = scriptUrl + "?FN=writeCol" + "&ID=" + sheetId + "&SH=" + sheetName + "&COL=" + columnNumber + "&DATA=" + data; String event = "writeCol"; GetFunction(params, event); } @SimpleFunction(description = "Enter A1 notation reference and values to write cell values to range") public void WriteRange(String ref, String data) { String params = scriptUrl + "?FN=writeRange" + "&ID=" + sheetId + "&SH=" + sheetName + "&REF=" + ref + "&DATA=" + data; String event = "writeRange"; GetFunction(params, event); } @SimpleFunction(description = "Append row to end and write row values") public void AppendRow(String data) { String params = scriptUrl + "?FN=appendRow" + "&ID=" + sheetId + "&SH=" + sheetName + "&DATA=" + data; String event = "appendRow"; GetFunction(params, event); } @SimpleFunction(description = "Append column to end and write column values") public void AppendColumn(String data) { String params = scriptUrl + "?FN=appendCol" + "&ID=" + sheetId + "&SH=" + sheetName + "&DATA=" + data; String event = "appendCol"; GetFunction(params, event); } @SimpleFunction(description = "Enter A1 notation reference and formula to set formula to cell") public void InsertFormula(String ref, String formula) { String params = scriptUrl + "?FN=insertFormula" + "&ID=" + sheetId + "&SH=" + sheetName + "&REF=" + ref + "&DATA=" + formula; String event = "insertFormula"; GetFunction(params, event); } @SimpleProperty(description = "The ScriptUrl to use") public String ScriptUrl() { return scriptUrl; } @SimpleProperty(description = "The SheetId to use") public String SheetId() { return sheetId; } @SimpleProperty(description = "The SheetName to use") public String SheetName() { return sheetName; } @DesignerProperty @SimpleProperty(description = "The ScriptUrl to use") public void ScriptUrl(String id) { if (!scriptUrl.equals(id)) { scriptUrl = id; } if (scriptUrl.equals("")){ throw new RuntimeException("scriptUrl property cannot be blank."); } } @DesignerProperty @SimpleProperty(description = "The SheetId to use") public void SheetId(String id) { if (!sheetId.equals(id)) { sheetId = id; } if (sheetId.equals("")){ throw new RuntimeException("sheetId property cannot be blank."); } } @DesignerProperty @SimpleProperty(description = "The SheetName to use") public void SheetName(String id) { if (!sheetName.equals(id)) { sheetName = id; } if (sheetName.equals("")){ throw new RuntimeException("sheetName property cannot be blank."); } } //General Get function private void GetFunction(String parameters, String event) { final String finalEvent = event; final String finalUrl = parameters; AsynchUtil.runAsynchronously(new Runnable() { @Override public void run() { try { final URL requestURL = new URL(finalUrl); HttpURLConnection httpURLConnection = (HttpURLConnection) requestURL.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(5000); BufferedReader bufferedReader = null; if (httpURLConnection.getResponseCode() == 200) { bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); } else { bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream())); } StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } bufferedReader.close(); final String responseContent = stringBuilder.toString(); activity.runOnUiThread(new Runnable() { @Override public void run() { try { GetFunctionOutput(responseContent, finalEvent); } catch (Exception e) { e.printStackTrace(); GetFunctionErrorOccurred(responseContent); } } }); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } //output event @SimpleEvent(description = "Event raised when output returned") public void GetFunctionOutput(final String output, final String event) { EventDispatcher.dispatchEvent(this, "GetFunctionOutput", output, event); } //error event @SimpleEvent(description = "Event raised when error occurred during execution.") public void GetFunctionErrorOccurred(final String errorMessage) { EventDispatcher.dispatchEvent(this, "GetFunctionErrorOccurred", errorMessage); } @SimpleFunction(description = "this function will decode a json object, e.g. an AI2 list or Dictionary, to a JSON string") public String JsonStringify(Object jsonObject) { try { return JsonUtil.encodeJsonObject(jsonObject); } catch (IllegalArgumentException e) { form.dispatchErrorOccurredEvent(this, "JsonStringify", ErrorMessages.ERROR_WEB_JSON_TEXT_ENCODE_FAILED, jsonObject); return ""; } } @SimpleFunction(description = "this function will encode a json or list as string to an object, e.g. an AI2 list") public Object JsonParse(String jsonText, boolean useDefault) { try { return ParseText(jsonText, useDefault); } catch (IllegalArgumentException e) { form.dispatchErrorOccurredEvent(this, "JsonParse", ErrorMessages.ERROR_WEB_JSON_TEXT_DECODE_FAILED, jsonText); return ""; } } public Object ParseText(String jsonText, boolean useDefault) throws IllegalArgumentException { try { return JsonUtil.getObjectFromJson(jsonText, useDefault); } catch (IllegalArgumentException | JSONException e) { throw new IllegalArgumentException("jsonText is not a legal JSON value"); } } //the end }