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

Publié presque 2 ans il y a par  Angélique Bernardo

Publier un sujet
A
Angélique Bernardo Administrateur

This JS snippet can help to build a spreadsheet of all locations/companies of an account, with a list of associated services.


Please note that it relies on jQuery.


<html>
<head>
    <title></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 + "/services?apikey=" + apiKey + "&token=" + token,
                    dataType: 'json',
                    success: function(data) {
                  var items = [];
                $.each(data.items, function(i,item){
                    subresult = [];
                    subresult.push("");
                    subresult.push("");
                    subresult.push("");
                    subresult.push(item.name);
                    subresult.push(item.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>Service</td><td>Link</td></thead>');
    document.write("<tbody>");
    odd = false;
    var companyId;
    var lang;
    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][3].length > 0) {
            document.write("<td><a href='https://app.agendize.com/book/" + companyId + "?service=" + resultApi[i][4] + "' 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