JS | Build a HTML table of scheduling widget links for all staffs and locations of an account

Publié sur un an il y a par  Julien Pauthier

Publier un sujet
Julien Pauthier
Julien Pauthier Administrateur

This JS snippet can help to build a spreadsheet of all locations/companies of an account, with a list of staffs and associated services. It can be an easy way to get a list of all links for staff's email signatures, for instance if you operate a franchise.


Please note that it relies on jQuery.


<html>
<head>
    <title>Extract</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<script type="text/javascript">

var apiKey = "";
var token = "";
var url = "https://app.agendize.com/api/2.1/scheduling/companies?apikey=" + apiKey + "&token=" + token;
var xhr = new XMLHttpRequest();
var resultApi = [];
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
    var subresult = [];
    if (xhr.readyState === 4 && xhr.status === 200) {
        var json = JSON.parse(xhr.responseText);
        for (i = 0; i < json.items.length; i++) {
            if (json.items[i].deleted == undefined) {
                subresult = [];
                subresult.push(json.items[i].id);
                subresult.push(json.items[i].name);
                subresult.push(json.items[i].phone);
                subresult.push("");
                subresult.push("");
                subresult.push("");
                subresult.push("");
                subresult.push("");
                resultApi.push(subresult);

                $.ajax({
                    type: 'GET',
                    url: "https://app.agendize.com/api/2.1/scheduling/companies/" + json.items[i].id + "/staff?apikey=" + apiKey + "&token=" + token,
                    dataType: 'json',
                    success: function(data) {
                  var items = [];
                $.each(data.items, function(i,item){
                    for (j = 0; j < item.services.length; j++) {
                        subresult = [];
                        subresult.push("");
                        subresult.push("");
                        subresult.push("");
                        subresult.push(item.firstName + " " + item.lastName);
                        subresult.push(item.email);
                        subresult.push(item.id);
                        subresult.push(item.services[j].name);
                        subresult.push(item.services[j].id);
                        resultApi.push(subresult);
                    }
                  });
                },
                    data: {},
                    async: false
                });
            }
        }

        process(resultApi);
    }
}
xhr.send();

function process(resultApi) {
    document.write("<body><table>");
    document.write('<thead style="font-weight:bold;background-color:#eee;"><td>Company name</td><td>Staff name</td><td>Staff email</td><td>Service</td><td>Link</td></thead>');
    document.write("<tbody>");
    odd = false;
    var companyId;
    var phone;
    for (i = 0; i < resultApi.length; i++) {
        document.write("<tr");
        if (odd) {      
                document.write(' style="background-color:#eee;"');
        }
        odd = !odd;
        if (resultApi[i][0].length > 0) {
            companyId = resultApi[i][0];
        }
        document.write(">");
        document.write("<td>" + resultApi[i][1] + "</td>");
        document.write("<td>" + resultApi[i][3] + "</td>");
        if (resultApi[i][6].length > 0) {
            document.write("<td><a href='https://app.agendize.com/book/" + companyId + "?staff=" + resultApi[i][5] + "' target='new'>" + resultApi[i][4] + "</a></td>");
        } else {
            document.write("<td></td>");
        }
        document.write("<td>" + resultApi[i][6] + "</td>");
        if (resultApi[i][6].length > 0) {
            document.write("<td><a href='https://app.agendize.com/book/" + companyId + "?staff=" + resultApi[i][5] + "&service=" + resultApi[i][7] + "' target='new'>Widget link</a></td>");
        } else {
            document.write("<td></td>");
        }
        document.write("</tr>");
    }
    document.write("</tbody>");
    document.write("</table></body></html>");
}
</script>


0 Votes


0 Commentaires

Connexion ou S'inscrire pour poster un commentaire