Skip to content

Commit a959241

Browse files
authored
Merge pull request #79 from ThomasPohl/feature/writeCell
Feature/write cell
2 parents 1a5ac34 + 4db090b commit a959241

File tree

16 files changed

+389
-9
lines changed

16 files changed

+389
-9
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ This adapter can be used to automatically interact with google spreadsheets.
2121
* [Create sheets](features/create-sheet.md)
2222
* [Delete sheets](features/delete-sheet.md)
2323
* [Duplicate sheets](features/duplicate-sheet.md)
24+
* [Read cell](features/read-cell.md)
25+
* [Write cell](features/write-cell.md)
2426

2527
## Usage
2628

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

99101

100102
## Changelog
103+
### 0.3.0
104+
* (Thomas Pohl) Added writing of single cells
105+
* (Thomas Pohl) Added reading of single cells
106+
* (Thomas Pohl) Documentation for all features
101107
### 0.2.0
102108
* (Thomas Pohl) Parsing of private keys is now more robust
103109

admin/blockly.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ if (typeof goog !== "undefined") {
88

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

11-
function getInstances(){
12-
var options = [[Blockly.Translate("google-spreadsheet_anyInstance"), ""]];
11+
function getInstances(withAny=true){
12+
var options = [];
13+
if (withAny){
14+
options.push([Blockly.Translate("google-spreadsheet_anyInstance"), ""]);
15+
}
1316
if (typeof main !== "undefined" && main.instances) {
1417
for (var i = 0; i < main.instances.length; i++) {
1518
var m = main.instances[i].match(/^system.adapter.google-spreadsheet.(\d+)$/);
@@ -45,5 +48,7 @@ loadJS("../google-spreadsheet/blocks/deleteRows.js");
4548
loadJS("../google-spreadsheet/blocks/createSheet.js");
4649
loadJS("../google-spreadsheet/blocks/deleteSheet.js");
4750
loadJS("../google-spreadsheet/blocks/duplicateSheet.js");
51+
loadJS("../google-spreadsheet/blocks/readCell.js");
52+
loadJS("../google-spreadsheet/blocks/writeCell.js");
4853

4954

admin/blocks/readCell.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"use strict";
2+
3+
/// --- Read Cell --------------------------------------------------
4+
5+
Blockly.Words["google-spreadsheet_read_read-from"] = { en: "read from", de: "Lies von" }
6+
Blockly.Words["google-spreadsheet_read_on-sheetName"] = { en: "sheet", de: "Tabellenblatt" }
7+
Blockly.Words["google-spreadsheet_read_in-cell"] = { en: "cell", de: "Zelle" }
8+
Blockly.Sendto.blocks["google-spreadsheet.read"] =
9+
'<block type="google-spreadsheet.read">' +
10+
' <value name="NAME">' +
11+
' </value>' +
12+
' <value name="INSTANCE">' +
13+
' </value>' +
14+
' <value name="SHEET_NAME">' +
15+
' <shadow type="text">' +
16+
' <field name="TEXT">text</field>' +
17+
' </shadow>' +
18+
' </value>' +
19+
' <value name="RANGE">' +
20+
' <shadow type="text">' +
21+
' <field name="TEXT">text</field>' +
22+
' </shadow>' +
23+
' </value>' +
24+
'</block>';
25+
26+
Blockly.Blocks["google-spreadsheet.read"] = {
27+
init: function () {
28+
const instances = getInstances(false);
29+
30+
this.appendDummyInput("NAME")
31+
.appendField(Blockly.Translate("google-spreadsheet_read_read-from"))
32+
.appendField(new Blockly.FieldDropdown(instances), "INSTANCE");
33+
34+
this.appendValueInput("SHEET_NAME")
35+
.appendField(Blockly.Translate("google-spreadsheet_read_on-sheetName"));
36+
37+
this.appendValueInput("CELL")
38+
.appendField(Blockly.Translate("google-spreadsheet_read_in-cell"));
39+
40+
41+
42+
this.setInputsInline(true);
43+
this.setPreviousStatement(false, null);
44+
this.setNextStatement(false, null);
45+
this.setOutput(true, 'String');
46+
47+
this.setColour(Blockly.Sendto.HUE);
48+
49+
},
50+
};
51+
52+
Blockly.JavaScript["google-spreadsheet.read"] = function (block) {
53+
var dropdown_instance = block.getFieldValue("INSTANCE");
54+
var data = Blockly.JavaScript.valueToCode(block, "DATA", Blockly.JavaScript.ORDER_ATOMIC);
55+
if (!data){
56+
data = "{}";
57+
}
58+
var sheetName = Blockly.JavaScript.valueToCode(block, "SHEET_NAME", Blockly.JavaScript.ORDER_ATOMIC);
59+
var cell = Blockly.JavaScript.valueToCode(block, "CELL", Blockly.JavaScript.ORDER_ATOMIC);
60+
61+
return ['await new Promise((resolve)=>{sendTo("google-spreadsheet' + dropdown_instance + '", "readCell", {"sheetName":"'+sheetName+'", "cell":"'+cell+'"}, (response)=>{resolve(response)}); })', 0];
62+
};
63+

admin/blocks/writeCell.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"use strict";
2+
3+
/// --- Write Cell --------------------------------------------------
4+
5+
Blockly.Words["google-spreadsheet_writeCell_write-to"] = { en: "write to", de: "Schreibe in " }
6+
Blockly.Words["google-spreadsheet_writeCell_sheetName"] = { en: "on sheet", de: "auf Tabellenblatt" }
7+
Blockly.Words["google-spreadsheet_writeCell_cell"] = { en: "in cell", de: "in Zelle" }
8+
Blockly.Words["google-spreadsheet_writeCell_data"] = { en: "the data", de: "die Daten" }
9+
10+
Blockly.Sendto.blocks["google-spreadsheet.writeCell"] =
11+
'<block type="google-spreadsheet.writeCell">' +
12+
13+
' <value name="NAME">' +
14+
' </value>' +
15+
' <value name="INSTANCE">' +
16+
' </value>' +
17+
' <value name="SHEET_NAME">' +
18+
' <shadow type="text">' +
19+
' <field name="TEXT">text</field>' +
20+
' </shadow>' +
21+
' </value>' +
22+
' <value name="CELL">' +
23+
' <shadow type="text">' +
24+
' <field name="TEXT">A1</field>' +
25+
' </shadow>' +
26+
' </value>' +
27+
' <value name="DATA">' +
28+
' </value>' +
29+
'</block>';
30+
31+
Blockly.Blocks["google-spreadsheet.writeCell"] = {
32+
init: function () {
33+
const instances = getInstances();
34+
35+
this.appendDummyInput("NAME")
36+
.appendField(Blockly.Translate("google-spreadsheet_writeCell_write-to"))
37+
.appendField(new Blockly.FieldDropdown(instances), "INSTANCE");
38+
39+
this.appendValueInput("SHEET_NAME")
40+
.appendField(Blockly.Translate("google-spreadsheet_writeCell_sheetName"));
41+
42+
this.appendValueInput("CELL")
43+
.appendField(Blockly.Translate("google-spreadsheet_writeCell_cell"));
44+
45+
this.appendValueInput("DATA")
46+
.appendField(Blockly.Translate("google-spreadsheet_writeCell_data"));
47+
48+
this.setInputsInline(false);
49+
this.setPreviousStatement(true, null);
50+
this.setNextStatement(true, null);
51+
52+
this.setColour(Blockly.Sendto.HUE);
53+
}
54+
};
55+
56+
Blockly.JavaScript["google-spreadsheet.writeCell"] = function (block) {
57+
var dropdown_instance = block.getFieldValue("INSTANCE");
58+
var sheetName = Blockly.JavaScript.valueToCode(block, "SHEET_NAME", Blockly.JavaScript.ORDER_ATOMIC);
59+
var cell = Blockly.JavaScript.valueToCode(block, "CELL", Blockly.JavaScript.ORDER_ATOMIC);
60+
var data = Blockly.JavaScript.valueToCode(block, "DATA", Blockly.JavaScript.ORDER_ATOMIC);
61+
62+
return 'sendTo("google-spreadsheet' + dropdown_instance + '", "writeCell", {"sheetName": '+sheetName+', "cell": '+cell+', "data":'+data+'}' + ");\n";
63+
};
64+

build/lib/google.js

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)