How to export a list of all the pages in your app to CSV

A client recently asked me export a list of all the pages in their Knack app to a CSV (to help them with documentation).

Here’s the method, in case others need it.

This method can be adapted to include much more information about your app (pages, views, fields etc). If you have the need for extra info in a CSV, let me know and I’ll demo how.

The code referenced in video to copy-paste into your browser is found here:

3 Likes

Hi Callum

Thanks for posting this I have used your code to get all the emails in our app to csv which is really useful I am posting it here in case anyone else finds it useful.

function getEmailsCsv() {
    const scenes = Knack.scenes.models;
    const hasEmails = view => Boolean(view.attributes?.rules?.emails?.length);

    const emails = scenes.flatMap(scene => {
        return scene.views.models.flatMap(view => {
            if (hasEmails(view)) {
                return view.attributes.rules.emails.map(rule => {
                    const email = rule.email;
                    return {
                        view_key: view.attributes.key, // Add the view key
                        from_email: email.from_email,
                        from_name: email.from_name,
                        message: email.message,
                        subject: email.subject,
                        recipients: email.recipients ? email.recipients.map(recipient => recipient.email || '').join(', ') : ''
                    };
                });
            } else {
                return [];
            }
        });
    });

    function convertToCSV(arr) {
        const array = [Object.keys(arr[0])].concat(arr);

        return array.map(row => {
            return Object.values(row).map(value => {
                let str = value.toString();
                str = str.replace(/\n/g, ' '); // Replace newline characters with space
                str = str.replace(/"/g, '""'); // Escape double quotes
                return str.includes(',') || str.includes('"') ? `"${str}"` : str; // Enclose fields with commas or double quotes in double quotes
            }).join(',');
        }).join('\n');
    }

    return convertToCSV(emails);
}

function downloadCSV(content, fileName) {
    const blob = new Blob([content], { type: 'text/csv;charset=utf-8;' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = fileName;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
}

function getFormattedDate() {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}`;
}

var csv = getEmailsCsv();
var dateStamp = getFormattedDate();
downloadCSV(csv, `emails_${dateStamp}.csv`);

Craig

1 Like

Brilliant, thanks to both of you - very useful indeed!

1 Like

Nice, thanks for sharing Craig!

1 Like