node-red-contrib-viseo-google-spreadsheet
Version:
VISEO Bot Maker - Google Sheets
245 lines (220 loc) • 12.1 kB
HTML
<script type="text/javascript">
RED.nodes.registerType('google-spreadsheet',{
category: '💾_data',
color: '#bce4d0',
defaults: {
name: { value: undefined },
auth: { value: undefined, type: 'google-service-account', required: true },
sheet: { value: undefined },
range: { value: undefined },
method: { value: 'append' },
direction: {value: 'line' },
action: { value: 'set' },
clear: {value: false},
line: { value: false},
column: { value: false},
fields: { value: 'all' },
save: { value: '_sheet'},
selfields: {value: [""]},
cell_l: { value: undefined},
cell_c: { value: undefined},
input: { value: undefined, required: false },
output: { value: undefined, required: false },
saveType: {value: 'global'},
inputType: {value: 'msg'},
outputType: {value: 'msg'},
sheetType: {value: 'str'},
rangeType: {value: 'str'},
cell_lType: {value: 'str'},
cell_cType: {value: 'str'}
},
inputs: 1,
outputs: 2,
icon: "sheets.png",
align: "left",
paletteLabel: 'Sheets',
label: function() { return this.name || "Google Sheets"; },
oneditprepare: function() {
$("#node-input-name").typedInput({ default: 'str', types: ['str'], type:'str' });
$("#node-input-sheet").typedInput({ default: 'str', types: ['str','msg','global'], typeField: $("#node-input-sheetType") });
$("#node-input-range").typedInput({ default: 'str', types: ['str','msg','global'], typeField: $("#node-input-rangeType") });
$("#node-input-cell_l").typedInput({ default: 'str', types: ['str','msg','global'], typeField: $("#node-input-cell_lType") });
$("#node-input-cell_c").typedInput({ default: 'str', types: ['str','msg','global'], typeField: $("#node-input-cell_cType") });
$("#node-input-save").typedInput({ default: 'global', types: ['msg','global'], typeField: $("#node-input-saveType") });
$("#node-input-input").typedInput({ default: 'msg', types: ['msg','global'], typeField: $("#node-input-inputType") });
$("#node-input-output").typedInput({ default: 'msg', types: ['msg','global'], typeField: $("#node-input-outputType") });
$("#node-input-action").change(function() {
$(".set, .get, .ctn, .cell").hide();
if ($(this).val() === "set") {
$(".set").show();
if ($("#node-input-fields").val() === "select") $(".ctn").show();
if ($("#node-input-method").val() === "new") $(".new").show();
$("#node-input-input").typedInput('show');
}
else if ($(this).val() === "get") $(".get").show();
else if ($(this).val() === "cell") {
$(".cell").show();
$("#node-input-cell_c").typedInput('show');
$("#node-input-cell_l").typedInput('show');
}
});
$("#node-input-fields").change(function() {
$(".ctn").hide();
if ($(this).val() === "select") $(".ctn").show();
});
$("#node-input-method").change(function(){
$(".new").hide();
if ($(this).val() === "new") $(".new").show();
})
$("#node-input-selfields-ctn").css('min-height','100px').editableList({
addItem: function ( row, index, data ) {
var content = data.field || '';
var hiddenField = $('<span/>', {}).appendTo(row);
var titleField = $('<input/>', { class:"node-input-property-ctn", type:"text", style:"width:90%", value: content, placeholder:"Key"}).appendTo(row);
},
sortable: true,
removable: true
});
for (var i=0; i<this.selfields.length; i++) {
var field = this.selfields[i];
$("#node-input-selfields-ctn").editableList('addItem', { field: field, i:i });
}
},
oneditsave: function(){
let fields = $("#node-input-selfields-ctn").editableList('items'),
results = new Array();
fields.each(function(i) {
var content = $(this).find('.node-input-property-ctn').val();
if (content) results.push(content);
});
this.selfields = (results.length === 0) ? [""] : results;
}
});
</script>
<style>
.input-info { font-size: 12px; padding-left: 104px; font-style: italic; }
textarea { resize: vertical; }
</style>
<script type="text/x-red" data-template-name="google-spreadsheet">
<div class="form-row" style="min-width:600px;">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" style="width:70%;" placeholder="Name">
</div>
<br>
<b>Google Spreadsheet settings</b>
<div class="form-row">
<br>
<label for="node-input-auth"><i class="fa fa-lock"></i> Credentials</label>
<input type="text" id="node-input-auth" style="width: 70%" placeholder="">
</div>
<div class="form-row">
<label for="node-input-sheet"><i class="fa fa-table"></i> ID</label>
<input type="text" id="node-input-sheet" style="width: 70%" placeholder="ID">
<input type="hidden" id="node-input-sheetType">
</div>
<div class="form-row">
<label for="node-input-range"><i class="fa fa-table"></i> Range</label>
<input type="text" id="node-input-range" style="width: 70%" placeholder="Tab label at least">
<input type="hidden" id="node-input-rangeType">
</div>
<div class="form-row">
<label for="node-input-save"><i class="fa fa-save"></i> Save</label>
<input type="text" id="node-input-save" style="width: 70%" placeholder="Saved data from spreadsheet">
<input type="hidden" id="node-input-saveType">
</div>
<br>
<b>Request settings</b>
<div class="form-row">
<br>
<label for="node-input-action"><i class="fa fa-tasks"></i> Action</label>
<select id="node-input-action" style="width: 34%">
<option value="set">Set data</option>
<option value="get">Get data</option>
<option value="cell">Get cell</option>
<option value="clear">Clear data</option>
</select>
<select class="set" id="node-input-method" style="width: 35%">
<option value="append">Append</option>
<option value="update">Update</option>
<option value="new">New</option>
</select>
<select class="get" id="node-input-direction" style="width: 35%">
<option value="line">By line</option>
<option value="column">By column</option>
</select>
</div>
<div class="form-row new get">
<label for="node-input-column"><i class="fa fa-tags"></i> Labels</label>
<input type="checkbox" style="width: auto; vertical-align:top;" id="node-input-line"> <span>First line for labels</span>
<input type="checkbox" style="width: auto; vertical-align:top; margin-left:25px;" id="node-input-column"> <span>First column for labels</span>
</div>
<div class="form-row set">
<label for="node-input-input"><i class="fa fa-sign-in"></i> Input</label>
<input type="text" id="node-input-input" style="width: 70%" placeholder="payload"/>
<input type="hidden" id="node-input-inputType">
</div>
<div class="form-row set">
<label for="node-input-fields"><i class="fa fa-tags"></i> Fields</label>
<select id="node-input-fields" style="width: 70%">
<option value="all">All</option>
<option value="select">Select</option>
</select>
</div>
<div class="form-row cell">
<label for="node-input-cell_c"><i class="fa fa-table"></i> Cell</label>
<input type="text" id="node-input-cell_c" style="width: 35%" placeholder="Column"/>
<input type="text" id="node-input-cell_l" style="width: 34%" placeholder="Line"/>
<input type="hidden" id="node-input-cell_lType">
<input type="hidden" id="node-input-cell_cType">
</div>
<div class="form-row ctn" style="width:70%; margin-left: 105px;">
<ol id="node-input-selfields-ctn"></ol>
<!-- <textarea id="node-input-selfields" rows="5" style="width:70%; margin-left: 105px;" placeholder="path.to.field"></textarea> -->
</div>
<div class="form-row">
<label for="node-input-output"><i class="fa fa-sign-out"></i> Output</label>
<input type="text" id="node-input-output" style="width: 70%" placeholder="payload"/>
<input type="hidden" id="node-input-outputType">
<!-- <div class="input-info">Output rows of the given range OR Input action results</div>-->
</div>
</script>
<script type="text/x-red" data-help-name="google-spreadsheet">
<p>Manipulate a Google spreadhseet with Sheets API. </p>
<h3>Details</h3>
<p>Properties</p>
<dl class="message-properties">
<dt>Credentials <span class="property-type">config</span></dt>
<dd>information from a Google service account</dd>
<dt>ID <span class="property-type">string</span></dt>
<dd>spreadsheet ID such as <i>https://docs.google.com/spreadsheets/d/<b>{ID}</b>/edit#gid=0</i></dd>
<dt>Range <span class="property-type">string</span></dt>
<dd>should at least be set as the name of the targeted spreadhseet's tab; ex: <i>Sheet1</i> or <i>Sheet1!A5:B8</i></dd>
<dt>Save <span class="property-type">string</span></dt>
<dd>where to save the data in order to avoid multiple calls to Sheets API</dd>
<dt>Output <span class="property-type">string</span></dt>
<dd>where to store the results</dd>
</dl>
<p><b>Note:</b> Please delete the content of the <i>Save</i> location every time the sheet's data has been changed!
This can be done with a simple "inject" node followed by a "change" node (delete <code>global._sheet</code>).
</p>
<p>
<code>Set data</code> updates or replaces (new) the value of the range, or append after. The <i>input</i> has to be an <b>array</b> or a <b>JSON object</b>. If the input
is an object, it is possible to use the object keys as labels, eg first line and/or column. If you chose the 'append' method, this will not impact the result.
</p>
<p>
<code>Get data</code> gets the value of the range. Check <i>labels</i> for line and/or column if the first line and/or the first column in your
spreadsheet is used for labels. The keys of the resulting objects will be adjusted accordingly.
</p>
<p>
<code>Get cell</code> gets the value of a cell. Precise line and column labels. Both should be precised. If nothing is found, the result is "Not found".
</p>
<h3>References</h3>
<ul>
<li><a href="https://developers.google.com/sheets/api/guides/concepts">Google Sheets</a> - reference</li>
<li><a href="https://github.com/NGRP/node-red-contrib-viseo/">VISEO BotMaker</a> - the nodes github repository</li>
</ul>
<h3>Tracking</h3>
<p>This node is likely to display an error message if no key is configured in the VISEO Bot Maker project.
If such an error appears, please get a key <a href="https://key.bot.viseo.io/">here</a>.
</p>
</script>