added doc export node
This commit is contained in:
parent
efcd8a0a5c
commit
77e8c08a25
4 changed files with 110 additions and 2 deletions
Binary file not shown.
67
node-red-grist/document-export.html
Normal file
67
node-red-grist/document-export.html
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
<script type="text/javascript">
|
||||
RED.nodes.registerType('grist-document-export', {
|
||||
|
||||
category: 'grist',
|
||||
color: '#00bb00',
|
||||
defaults: {
|
||||
name: { value: "", required: false },
|
||||
server: { value: "", type: "grist-server", required: true },
|
||||
document: { value: "", type: "grist-document", required: true },
|
||||
format: { value: "excel", required: true },
|
||||
},
|
||||
inputs: 1,
|
||||
outputs: 1,
|
||||
icon: "font-awesome/fa-download",
|
||||
label: function () {
|
||||
return this.name ? this.name : "Export document";
|
||||
},
|
||||
paletteLabel: "Export document",
|
||||
oneditprepare: function () {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<script type="text/html" data-template-name="grist-document-export">
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-document"><i class="fa fa-file-text"></i> Document</label>
|
||||
<input type="text" id="node-input-document" placeholder="Document">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-server"><i class="fa fa-server"></i> Server</label>
|
||||
<input type="text" id="node-input-server" placeholder="Server">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-format"><i class="fa fa-file-o"></i> Format</label>
|
||||
<select id="node-input-format" name="node-input-format">
|
||||
<option value="sqlite">SQLite</option>
|
||||
<option value="excel">Excel</option>
|
||||
</select>
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/html" data-help-name="grist-document-export">
|
||||
<p>Exports a document in various formats</p>
|
||||
<h3>Inputs</h3>
|
||||
<p>
|
||||
|
||||
</p>
|
||||
<h3>Outputs</h3>
|
||||
<p>
|
||||
<ul>
|
||||
<li><code>msg.payload.data</code> Binary buffer containing the file contents</li>
|
||||
<li><code>msg.payload.format</code> Selected export format</li>
|
||||
</ul>
|
||||
</p>
|
||||
<h3>Attributes</h3>
|
||||
<p>
|
||||
<code>Format</code> set the export format
|
||||
</p>
|
||||
|
||||
</script>
|
||||
39
node-red-grist/document-export.js
Normal file
39
node-red-grist/document-export.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
|
||||
|
||||
module.exports = function (RED) {
|
||||
function DocumentExportNode(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
let node = this;
|
||||
this.document = RED.nodes.getNode(config.document);
|
||||
this.server = RED.nodes.getNode(config.server);
|
||||
this.format = config.format
|
||||
|
||||
node.on('input', async function (msg, send, done) {
|
||||
const protocol = this.server.tlsEnabled === true ? "https" : "http";
|
||||
const url = protocol + "://" + this.server.hostname + ":" + this.server.port;
|
||||
|
||||
var requestUrl;
|
||||
switch (this.format) {
|
||||
case "excel": requestUrl = `${url}/api/docs/${this.document.docid}/download`
|
||||
break;
|
||||
case "sqlite": requestUrl = `${url}/api/docs/${this.document.docid}/download/xlsx`
|
||||
break;
|
||||
default:
|
||||
done("Unsupported format " + this.format);
|
||||
}
|
||||
|
||||
fetch(requestUrl, {
|
||||
headers: {
|
||||
'authorization': `Bearer ${this.server.apiKey}`
|
||||
}
|
||||
}).then(async (response) => {
|
||||
const blob = await response.blob()
|
||||
const arrayBuffer = await blob.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
node.send({ ...msg, payload: buffer })
|
||||
|
||||
}).catch(reason => done(reason, "Failed to perform grist request to " + url));
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("grist-document-export", DocumentExportNode);
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@gorootde/node-red-grist",
|
||||
"version": "2.0.1",
|
||||
"version": "2.1.0",
|
||||
"description": "getgrist.com connectivity for NodeRed",
|
||||
"main": "index.js",
|
||||
"packageManager": "yarn@3.3.1",
|
||||
|
|
@ -19,6 +19,7 @@
|
|||
"node-red": {
|
||||
"version": ">=2.0.0",
|
||||
"nodes": {
|
||||
"grist-document-export": "node-red-grist/document-export.js",
|
||||
"grist-add-records": "node-red-grist/add-records.js",
|
||||
"grist-get-records": "node-red-grist/get-records.js",
|
||||
"grist-update-records": "node-red-grist/update-records.js",
|
||||
|
|
@ -31,6 +32,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"grist-api": "^0.1.7",
|
||||
"mustache": "^4.2.0"
|
||||
"mustache": "^4.2.0",
|
||||
"node-fetch": "^3.3.2"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue