-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwriteCells.js
More file actions
90 lines (79 loc) · 3.62 KB
/
writeCells.js
File metadata and controls
90 lines (79 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
/*global Blockly */
/*global getInstances */
/// --- Write Cells --------------------------------------------------
Blockly.Words['google-spreadsheet_writeCells_write-to'] = { en: 'write cells to', de: 'Schreibe Zellen in ' };
Blockly.Words['google-spreadsheet_writeCells_cells'] = { en: 'cells', de: 'Zellen' };
Blockly.Sendto.blocks['google-spreadsheet.writeCells'] =
'<block type="google-spreadsheet.writeCells">' +
' <field name="INSTANCE"></field>' +
' <statement name="CELLS"></statement>' +
'</block>';
// addCell-Block in die Toolbox aufnehmen, damit er auswählbar ist
Blockly.Sendto.blocks['google-spreadsheet.addCell'] =
'<block type="google-spreadsheet.addCell">' +
' <field name="SHEET_NAME">' +
' <shadow type="text">' +
' <field name="TEXT">text</field>' +
' </shadow>' +
' </field>' +
' <field name="CELL">' +
' <shadow type="text">' +
' <field name="TEXT">A1</field>' +
' </shadow>' +
' </field>' +
' <field name="DATA">' +
' <shadow type="text">' +
' <field name="TEXT"></field>' +
' </shadow>' +
' </field>' +
'</block>';
Blockly.Blocks['google-spreadsheet.writeCells'] = {
init: function () {
const instances = getInstances();
this.appendDummyInput('NAME')
.appendField(Blockly.Translate('google-spreadsheet_writeCells_write-to'))
.appendField(new Blockly.FieldDropdown(instances), 'INSTANCE');
this.appendStatementInput('CELLS')
.setCheck('google-spreadsheet.writeCell')
.appendField(Blockly.Translate('google-spreadsheet_writeCells_cells'));
this.setInputsInline(false);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(Blockly.Sendto.HUE);
},
};
Blockly.JavaScript.forBlock['google-spreadsheet.writeCells'] = function (block) {
const dropdown_instance = block.getFieldValue('INSTANCE');
const cellsCode = Blockly.JavaScript.statementToCode(block, 'CELLS');
// cellsCode enthält mehrere Zeilen wie: addCell({sheetName: ..., cell: ..., data: ...});
// Wir sammeln die Argumente in ein Array
const cells = [];
const cellRegex = /addCell\((\{[^}]+\})\);/g;
let match;
while ((match = cellRegex.exec(cellsCode)) !== null) {
try {
cells.push(eval(`(${match[1]})`));
} catch (e) {
console.error('Error parsing cell data:', e);
}
}
return `sendTo("google-spreadsheet${dropdown_instance}", "writeCells", {cells: ${JSON.stringify(cells)}});\n`;
};
// Hilfsblock für einzelne Zelleingabe (nur für writeCells)
Blockly.Blocks['google-spreadsheet.addCell'] = {
init: function () {
this.appendValueInput('SHEET_NAME').appendField('Sheet');
this.appendValueInput('CELL').appendField('Cell');
this.appendValueInput('DATA').appendField('Data');
this.setPreviousStatement(true, 'google-spreadsheet.writeCell');
this.setNextStatement(true, 'google-spreadsheet.writeCell');
this.setColour(Blockly.Sendto.HUE);
},
};
Blockly.JavaScript.forBlock['google-spreadsheet.addCell'] = function (block) {
const sheetName = Blockly.JavaScript.valueToCode(block, 'SHEET_NAME', Blockly.JavaScript.ORDER_ATOMIC);
const cell = Blockly.JavaScript.valueToCode(block, 'CELL', Blockly.JavaScript.ORDER_ATOMIC);
const data = Blockly.JavaScript.valueToCode(block, 'DATA', Blockly.JavaScript.ORDER_ATOMIC);
return `addCell({sheetName: ${sheetName}, cell: ${cell}, data: ${data}});\n`;
};