Created
January 19, 2019 05:36
-
-
Save jeznag/ef80ac366842072d2430b3e8ddf232d5 to your computer and use it in GitHub Desktop.
Example Zoho CRM widget to allow TSV data downloads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<style> | |
[data-download] { | |
color: blue; | |
} | |
[data-download][href="#"] { | |
color: grey; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>TSV Reports</h2> | |
<a download="report-tsv.tsv" href="#">Download TSV</a> | |
<script src="ZohoEmbededAppSDK.min.js"></script> | |
<script src="generate-tsv-links.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In order to facilitate the download, we're going to allocate Object URLs. | |
// We'll need to keep track of those so we can manage the browser memory. | |
let downloadUrl = null; | |
const downloadLink = document.querySelector('a[download]'); | |
ZOHO.embeddedApp.on("PageLoad", function(data){ | |
ZOHO.CRM.API.getAllRecords({Entity:"Leads",sort_order:"asc",per_page:2,page:1}) | |
.then(function(data) { | |
updateDownloadHref(data.data); | |
console.log(data) | |
}) | |
.catch((e) => { | |
debugger; | |
}); | |
}); | |
ZOHO.embeddedApp.init(); | |
function generateTsv(leads) { | |
const leadProperties = Object.keys(leads[0]); | |
const tsvHeaders = leadProperties.join('\t'); | |
const tsvRows = leads.map((lead) => { | |
return Object.values(lead).map((leadValue) => JSON.stringify(leadValue)).join('\t') | |
}).join('\n'); | |
return `${tsvHeaders}\n${tsvRows}\n`; | |
} | |
function updateDownloadHref(leadData) { | |
// Create a binary representation of the plain-text input. | |
const tsvData = generateTsv(leadData); | |
const blob = new Blob( | |
[tsvData], // Blob parts. | |
{ | |
type : "text/tsv;charset=utf-8" | |
} | |
); | |
// When we create Object URLs, the browser will keep them in memory until the | |
// document is unloaded or until the URL is explicitly released. Since we are | |
// going to create a new URL every time the user hits a key-stroke (in this | |
// particular demo), we need to be sure to release the previous Object URL | |
// before we create the new one. | |
if ( downloadUrl ) { | |
URL.revokeObjectURL( downloadUrl ); | |
} | |
// Create an addressable version of the blob. | |
// -- | |
// CAUTION: At this point, the URL has been allocated and the blob will be | |
// kept in the document memory space until the document is unloaded or the | |
// URL is explicitly released (see above). | |
downloadUrl = URL.createObjectURL( blob ); | |
// Tie the addressable version of the blob to the download link. | |
downloadLink.setAttribute( "href", downloadUrl ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment