在 Qlik Cloud 中以 JSON 文件形式获取 Qlik 仪表板的结构/布局的最简单方法是什么?因此它应该包含有关可视化和样式的详细信息。
我已经可以使用如何获取 XML/JSON 文件形式的 Qlik 应用程序布局?中的脚本从 Qlik Sense Desktop 版本中提取它。但如何直接从 Qlik Cloud 提取布局信息?
我的脚本已经可以打开应用程序了。
(async () => {
const { docMixin } = await import("enigma-mixin");
const enigma = require('enigma.js');
const schema = require('enigma.js/schemas/12.20.0.json');
const WebSocket = require('ws');
const fs = require('fs');
require('dotenv').config();
const QCS_HOST = process.env.QCS_HOST || 'host-id.de.qlikcloud.com';
const QCS_API_KEY = process.env.QCS_API_KEY || 'my-API-Key';
const appId = 'ee536af8-6e5a-4b97-b8af-8aedec556d10'; // App-ID anpassen
const filePath = 'entities.json'; // Dateipfad für die gespeicherten Entitäten
const session = enigma.create({
schema,
mixins: [...docMixin],
url: `wss://${QCS_HOST}/app/${appId}`,
createSocket: (url) => new WebSocket(url, {
headers: { Authorization: `Bearer ${QCS_API_KEY}` },
}),
});
async function run() {
try {
const global = await session.open();
console.log('Verbindung erfolgreich hergestellt');
let doc;
// Versuche, das Dokument zu öffnen
try {
doc = await global.openDoc(appId);
console.log('Dokument erfolgreich geöffnet');
} catch (error) {
if (error.code === 1002) {
// Fehler "App already open", wir holen die bereits geöffnete App
console.log('Die App ist bereits geöffnet');
doc = await global.getActiveDoc();
console.log('Referenz zur bereits geöffneten App geholt');
} else {
throw error; // Anderen Fehler weitergeben
}
}
// Abrufen der Entitäten
const entities = await doc.mUnbuild();
// Entitäten in eine Datei speichern
saveEntitiesToFile(entities);
await session.close();
} catch (error) {
console.error('Fehler während der Ausführung:', error);
}
}
function saveEntitiesToFile(entities) {
// Konvertierung der Entitäten in einen JSON-String mit Einrückung
const entitiesJSON = JSON.stringify(entities, null, 2);
// Schreiben der Entitäten in die Datei
fs.writeFile(filePath, entitiesJSON, (err) => {
if (err) {
console.error('Fehler beim Speichern der Entities:', err);
return;
}
console.log('Entities erfolgreich gespeichert.');
});
}
run();
})();
Verbindung erfolgreich hergestellt
Dokument erfolgreich geöffnet
Fehler während der Ausführung: TypeError: obj.getParent is not a function
at file:///c:/Users/j.stoeffler/Documents/git/BI_Migration/node_modules/enigma-mixin/dist/index.js:838:14
at Generator.next (<anonymous>)
at fulfilled (file:///c:/Users/j.stoeffler/Documents/git/BI_Migration/node_modules/enigma-mixin/dist/index.js:21:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
设置您的环境: 确保您已安装必要的软件包:
npm 安装 enigma.js enigma-mixin ws dotenv
更新脚本以正确获取布局信息:
(async () => { const { docMixin } = await import("enigma-mixin"); const enigma = require('enigma.js'); const schema = require('enigma.js/schemas/12.170.2.json'); // Update to the latest schema if necessary const WebSocket = require('ws'); const fs = require('fs');
require('dotenv').config();
const QCS_HOST = process.env.QCS_HOST || 'host-id.de.qlikcloud.com'; const QCS_API_KEY = process.env.QCS_API_KEY || 'my-API-Key';
const appId = 'ee536af8-6e5a-4b97-b8af-8aedec556d10'; // Adjust App-ID const filePath = 'entities.json'; // Path for saving the entities
const session = enigma.create({
schema,
mixins: [...docMixin],
url: `wss://${QCS_HOST}/app/${appId}`,
createSocket: (url) => new WebSocket(url, {
headers: { Authorization: `Bearer ${QCS_API_KEY}` },
}), });
async function run() {
try {
const global = await session.open();
console.log('Connection established successfully');
let doc;
// Try to open the document
try {
doc = await global.openDoc(appId);
console.log('Document opened successfully');
} catch (error) {
if (error.code === 1002) {
// Error "App already open", get the already opened app
console.log('The app is already open');
doc = await global.getActiveDoc();
console.log('Reference to the already opened app retrieved');
} else {
throw error; // Propagate other errors
}
}
// Fetching the layout
const layout = await doc.getLayout();
// Save layout to a file
saveEntitiesToFile(layout);
await session.close();
} catch (error) {
console.error('Error during execution:', error);
} }
function saveEntitiesToFile(entities) {
// Convert entities to a JSON string with indentation
const entitiesJSON = JSON.stringify(entities, null, 2);
// Write the entities to the file
fs.writeFile(filePath, entitiesJSON, (err) => {
if (err) {
console.error('Error saving entities:', err);
return;
}
console.log('Entities saved successfully.');
}); }
run(); })();
确保您的 .env 文件包含正确的 QCS 主机和 API 密钥,
QCS_HOST=您的 qlik-云主机 QCS_API_KEY=您的 api 密钥
运行脚本:
节点 your-script-file.js
希望有帮助..