Skip to content

Commit

Permalink
Merge pull request #79 from ThomasPohl/feature/writeCell
Browse files Browse the repository at this point in the history
Feature/write cell
  • Loading branch information
ThomasPohl authored May 21, 2024
2 parents 1a5ac34 + 4db090b commit a959241
Show file tree
Hide file tree
Showing 16 changed files with 389 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ This adapter can be used to automatically interact with google spreadsheets.
* [Create sheets](features/create-sheet.md)
* [Delete sheets](features/delete-sheet.md)
* [Duplicate sheets](features/duplicate-sheet.md)
* [Read cell](features/read-cell.md)
* [Write cell](features/write-cell.md)

## Usage

Expand Down Expand Up @@ -98,6 +100,10 @@ Make sure the Service Account has adequate permissions to write to the spreadshe


## Changelog
### 0.3.0
* (Thomas Pohl) Added writing of single cells
* (Thomas Pohl) Added reading of single cells
* (Thomas Pohl) Documentation for all features
### 0.2.0
* (Thomas Pohl) Parsing of private keys is now more robust

Expand Down
9 changes: 7 additions & 2 deletions admin/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ if (typeof goog !== "undefined") {

Blockly.Words["google-spreadsheet_anyInstance"] = { en: "all instances", de: "allen Instanzen" };

function getInstances(){
var options = [[Blockly.Translate("google-spreadsheet_anyInstance"), ""]];
function getInstances(withAny=true){
var options = [];
if (withAny){
options.push([Blockly.Translate("google-spreadsheet_anyInstance"), ""]);
}
if (typeof main !== "undefined" && main.instances) {
for (var i = 0; i < main.instances.length; i++) {
var m = main.instances[i].match(/^system.adapter.google-spreadsheet.(\d+)$/);
Expand Down Expand Up @@ -45,5 +48,7 @@ loadJS("../google-spreadsheet/blocks/deleteRows.js");
loadJS("../google-spreadsheet/blocks/createSheet.js");
loadJS("../google-spreadsheet/blocks/deleteSheet.js");
loadJS("../google-spreadsheet/blocks/duplicateSheet.js");
loadJS("../google-spreadsheet/blocks/readCell.js");
loadJS("../google-spreadsheet/blocks/writeCell.js");


63 changes: 63 additions & 0 deletions admin/blocks/readCell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"use strict";

/// --- Read Cell --------------------------------------------------

Blockly.Words["google-spreadsheet_read_read-from"] = { en: "read from", de: "Lies von" }
Blockly.Words["google-spreadsheet_read_on-sheetName"] = { en: "sheet", de: "Tabellenblatt" }
Blockly.Words["google-spreadsheet_read_in-cell"] = { en: "cell", de: "Zelle" }
Blockly.Sendto.blocks["google-spreadsheet.read"] =
'<block type="google-spreadsheet.read">' +
' <value name="NAME">' +
' </value>' +
' <value name="INSTANCE">' +
' </value>' +
' <value name="SHEET_NAME">' +
' <shadow type="text">' +
' <field name="TEXT">text</field>' +
' </shadow>' +
' </value>' +
' <value name="RANGE">' +
' <shadow type="text">' +
' <field name="TEXT">text</field>' +
' </shadow>' +
' </value>' +
'</block>';

Blockly.Blocks["google-spreadsheet.read"] = {
init: function () {
const instances = getInstances(false);

this.appendDummyInput("NAME")
.appendField(Blockly.Translate("google-spreadsheet_read_read-from"))
.appendField(new Blockly.FieldDropdown(instances), "INSTANCE");

this.appendValueInput("SHEET_NAME")
.appendField(Blockly.Translate("google-spreadsheet_read_on-sheetName"));

this.appendValueInput("CELL")
.appendField(Blockly.Translate("google-spreadsheet_read_in-cell"));



this.setInputsInline(true);
this.setPreviousStatement(false, null);
this.setNextStatement(false, null);
this.setOutput(true, 'String');

this.setColour(Blockly.Sendto.HUE);

},
};

Blockly.JavaScript["google-spreadsheet.read"] = function (block) {
var dropdown_instance = block.getFieldValue("INSTANCE");
var data = Blockly.JavaScript.valueToCode(block, "DATA", Blockly.JavaScript.ORDER_ATOMIC);
if (!data){
data = "{}";
}
var sheetName = Blockly.JavaScript.valueToCode(block, "SHEET_NAME", Blockly.JavaScript.ORDER_ATOMIC);
var cell = Blockly.JavaScript.valueToCode(block, "CELL", Blockly.JavaScript.ORDER_ATOMIC);

return ['await new Promise((resolve)=>{sendTo("google-spreadsheet' + dropdown_instance + '", "readCell", {"sheetName":"'+sheetName+'", "cell":"'+cell+'"}, (response)=>{resolve(response)}); })', 0];
};

64 changes: 64 additions & 0 deletions admin/blocks/writeCell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"use strict";

/// --- Write Cell --------------------------------------------------

Blockly.Words["google-spreadsheet_writeCell_write-to"] = { en: "write to", de: "Schreibe in " }
Blockly.Words["google-spreadsheet_writeCell_sheetName"] = { en: "on sheet", de: "auf Tabellenblatt" }
Blockly.Words["google-spreadsheet_writeCell_cell"] = { en: "in cell", de: "in Zelle" }
Blockly.Words["google-spreadsheet_writeCell_data"] = { en: "the data", de: "die Daten" }

Blockly.Sendto.blocks["google-spreadsheet.writeCell"] =
'<block type="google-spreadsheet.writeCell">' +

' <value name="NAME">' +
' </value>' +
' <value name="INSTANCE">' +
' </value>' +
' <value name="SHEET_NAME">' +
' <shadow type="text">' +
' <field name="TEXT">text</field>' +
' </shadow>' +
' </value>' +
' <value name="CELL">' +
' <shadow type="text">' +
' <field name="TEXT">A1</field>' +
' </shadow>' +
' </value>' +
' <value name="DATA">' +
' </value>' +
'</block>';

Blockly.Blocks["google-spreadsheet.writeCell"] = {
init: function () {
const instances = getInstances();

this.appendDummyInput("NAME")
.appendField(Blockly.Translate("google-spreadsheet_writeCell_write-to"))
.appendField(new Blockly.FieldDropdown(instances), "INSTANCE");

this.appendValueInput("SHEET_NAME")
.appendField(Blockly.Translate("google-spreadsheet_writeCell_sheetName"));

this.appendValueInput("CELL")
.appendField(Blockly.Translate("google-spreadsheet_writeCell_cell"));

this.appendValueInput("DATA")
.appendField(Blockly.Translate("google-spreadsheet_writeCell_data"));

this.setInputsInline(false);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);

this.setColour(Blockly.Sendto.HUE);
}
};

Blockly.JavaScript["google-spreadsheet.writeCell"] = function (block) {
var dropdown_instance = block.getFieldValue("INSTANCE");
var sheetName = Blockly.JavaScript.valueToCode(block, "SHEET_NAME", Blockly.JavaScript.ORDER_ATOMIC);
var cell = Blockly.JavaScript.valueToCode(block, "CELL", Blockly.JavaScript.ORDER_ATOMIC);
var data = Blockly.JavaScript.valueToCode(block, "DATA", Blockly.JavaScript.ORDER_ATOMIC);

return 'sendTo("google-spreadsheet' + dropdown_instance + '", "writeCell", {"sheetName": '+sheetName+', "cell": '+cell+', "data":'+data+'}' + ");\n";
};

38 changes: 38 additions & 0 deletions build/lib/google.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a959241

Please sign in to comment.