SAMPLEPROFILE Component

Function SAMPLEPROFILE

SAMPLEPROFILE(year, UID, dataid, pkey)

The function creates a sample of the recently uploaded data and call the EMAILSAMPLEPROFILE(dataid, subject, message0, summarytable, UID) at the end.

Arguments
  • year (string) – the current year

  • UID (string) – the current user

  • dataid (string) – id of data (templatid on ALLDATA sheet)

  • pkey (Object) – the list of key properties and their values

Returns

keyinputs – List of key parameters.

  • Google Drive Folder

This is the image caption

The screenshot of Google Drive Folder

This is the image caption

The screenshot of Summary sheet

  • Diagram

This is the image caption

The block diagram of SAMPLEPROFILE component

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function SAMPLEPROFILE(year, UID, dataid, pkey) {
    if (pkey != null) { key = pkey };
     /* mainfolderid {string}  - id of main folder */
    var mainfolderid = createFolder(0, key["MAINFOLDER"]);
    /*Sfile  {string} - the id of created file [GENERAL Component]*/
    var Sfile = createFile(key["OTHERFOLDERID"], "SAMPLEPROFILE");
    var hss = SpreadsheetApp.openById(Sfile);
    var datasheet = hss.getSheetByName("DATA");
    var sheetbg = hss.getSheetByName("SUMMARY");

    if (year < 2017 || year == null) { return; }

    var FID;
  • Survey indentification

This is the image caption

The screenshot of Bsurvey sheet

1
2
3
4
5
6
7
8
9
    var data = getData("BSURVEY");
    for (i = 1; i < data.length; i++) {
        if (year == data[i][data[0].indexOf("YEAR")] &&
            data[i][data[0].indexOf("title")].indexOf("TRAINING") == -1 &&
            data[i][data[0].indexOf("title")].indexOf("Partner") == -1) {
            FID = data[i][data[0].indexOf("formid")];
            break;
        }
    }
  • Load data for current user and form id

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    var querystring = '{"TemplateID":"' + dataid + '"}'
    var query2 = encodeURIComponent(querystring);
    Logger.log(query2);
    var url = 'https://kobocat.unhcr.org/api/v1/data/' + FID + '?query=' + query2;
    Logger.log(url);
    var option = {
        "method": "get",
        "headers": {
            "Authorization": "Token " + key["APITOKEN2"]
        }
    };

    try { var json = UrlFetchApp.fetch(url, option); } catch (e) { var error = e; Logger.log(error.message); }
    var jsonData = JSON.parse(json.getContentText());

    var lr = sheetbg.getLastRow() + 1;
    var country = [];
    var partner = [];
    var camp = [];
    var status = [];
    var gender = [];
    var age = [];
    var year = [];
    var host = [];
    var ii = 0,
        iii = 0,
        iiii = 0,
        i5 = 0,
        i6 = 0,
        i7 = 0,
        i8 = 0,
        i9 = 0;
    var age1 = 18,
        age2 = 65,
        agecount1 = 0,
        agecount2 = 0,
        agecount3 = 0;
    var agecount11 = 0,
        agecount22 = 0,
        agecount33 = 0;
    var agecount111 = 0,
        agecount222 = 0,
        agecount333 = 0;

    sheetbg.getRange(1, 1, lr + 1, 20).clear();

    for (i = 0; i < jsonData.length; i++) {

        if (country.indexOf(jsonData[i]["Country"]) == -1) {
            country[ii] = jsonData[i]["Country"];
            ii++;
        }

        if (partner.indexOf(jsonData[i]["PARTNER_INFO/PARTNER"]) == -1) {
            partner[iii] = jsonData[i]["PARTNER_INFO/PARTNER"];
            iii++;
        }

        if (jsonData[i]["PARTNER_INFO/Camp"] != null && jsonData[i]["PARTNER_INFO/Camp"] != 0 && camp.indexOf(jsonData[i]["PARTNER_INFO/Camp"]) == -1) {
            camp[iiii] = jsonData[i]["PARTNER_INFO/Camp"];
            iiii++;
        } else {
            if (jsonData[i]["PARTNER_INFO/Camp"] == "0" && camp.indexOf("Other") == -1) {
                camp[iiii] = "Other";
                iiii++;
            }
        }

        if (status.indexOf(jsonData[i]["BENEFICIARY_INFO/Status"]) == -1) {
            status[i5] = jsonData[i]["BENEFICIARY_INFO/Status"];
            i5++;
        }

        if (gender.indexOf(jsonData[i]["BENEFICIARY_INFO/Gender"]) == -1) {
            gender[i6] = jsonData[i]["BENEFICIARY_INFO/Gender"];
            i6++;
        }

        if (jsonData[i]["BENEFICIARY_INFO/Age"] < 18) {
            if (jsonData[i]["PARTNER_INFO/BE"] == "Baseline") { agecount1++; }
            if (jsonData[i]["PARTNER_INFO/BE"] == "Midline") { agecount111++; }
            if (jsonData[i]["PARTNER_INFO/BE"] == "Endline") { agecount11++; }
            if (age.indexOf("Under 18") == -1) {
                age[i7] = "Under 18";
                i7++;
            }
        } else {
            if (jsonData[i]["BENEFICIARY_INFO/Age"] > 65) {
                if (jsonData[i]["PARTNER_INFO/BE"] == "Baseline") { agecount2++; }
                if (jsonData[i]["PARTNER_INFO/BE"] == "Midline") { agecount222++; }
                if (jsonData[i]["PARTNER_INFO/BE"] == "Endline") { agecount22++; }
                if (age.indexOf("Over 65") == -1) {
                    age[i7] = "Over 65";
                    i7++;
                }
            } else {
                if (jsonData[i]["PARTNER_INFO/BE"] == "Baseline") { agecount3++; }
                if (jsonData[i]["PARTNER_INFO/BE"] == "Midline") { agecount333++; }
                if (jsonData[i]["PARTNER_INFO/BE"] == "Endline") { agecount33++; }
                if (age.indexOf("18-65") == -1) {
                    age[i7] = "18-65";
                    i7++;
                }
            }
        }

        if (year.indexOf(jsonData[i]["MonitoringYear"]) == -1) {
            year[i8] = jsonData[i]["MonitoringYear"];
            i8++;
        }

        if (jsonData[i]["PARTNER_INFO/Nearby_Camp"] != null && jsonData[i]["PARTNER_INFO/Nearby_Camp"] != 0 && host.indexOf(jsonData[i]["PARTNER_INFO/Nearby_Camp"]) == -1) {
            host[i9] = jsonData[i]["PARTNER_INFO/Nearby_Camp"];
            i9++;
        } else {
            if (jsonData[i]["PARTNER_INFO/Nearby_Camp"] != "" && host.indexOf("Other") == -1) {
                host[i9] = "Other";
                i9++;
            }
        }
    }

    var y = 1;
    sheetbg.getRange(y, 1).setValue("SAMPLE PROFILE - " + year); y++; y++;

    sheetbg.getRange(y, 1).setValue("OVERALL");
    sheetbg.getRange(y, 2).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspBaseline");
    sheetbg.getRange(y, 3).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMidline");
    sheetbg.getRange(y, 4).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspEndline"); y++;
    var output = [];
    for (ia = 0; ia < country.length; ia++) {
        output[ia] = new Array();
        output[ia][0] = country[ia];
        /*countx (item, header, category, line, contain)*/
        output[ia][1] = Countx(jsonData, "Country", [[country[ia]][0]], "Baseline", 0);
        output[ia][2] = Countx(jsonData, "Country", [[country[ia]][0]], "Midline", 0);
        output[ia][3] = Countx(jsonData, "Country", [[country[ia]][0]], "Endline", 0);
    }
    sheetbg.getRange(y, 1, country.length, 4).setValues(output);
    y = y + country.length + 1;

    sheetbg.getRange(y, 1).setValue("● PARTNER");
    sheetbg.getRange(y, 2).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspBaseline");
    sheetbg.getRange(y, 3).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMidline");
    sheetbg.getRange(y, 4).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspEndline"); y++;
    var output2 = [];
    for (ia = 0; ia < partner.length; ia++) {
        output2[ia] = new Array();
        output2[ia][0] = partner[ia];
        output2[ia][1] = Countx(jsonData, "PARTNER_INFO/PARTNER", [[partner[ia]]], "Baseline", 0);
        output2[ia][2] = Countx(jsonData, "PARTNER_INFO/PARTNER", [[partner[ia]]], "Midline", 0);
        output2[ia][3] = Countx(jsonData, "PARTNER_INFO/PARTNER", [[partner[ia]]], "Endline", 0);
    }
    /*tsort [Data Analysis]*/
    output2 = tsort(output2, 1);
    sheetbg.getRange(y, 1, partner.length, 4).setValues(output2);
    y = y + partner.length + 1;

    sheetbg.getRange(y, 1).setValue("● STATUS");
    sheetbg.getRange(y, 2).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspBaseline");
    sheetbg.getRange(y, 3).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMidline");
    sheetbg.getRange(y, 4).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspEndline"); y++;
    var output4 = [];
    for (id = 0; id < status.length; id++) {
        output4[id] = new Array();
        output4[id][0] = status[id];
        output4[id][1] = Countx(jsonData, "BENEFICIARY_INFO/Status", [[status[id]]], "Baseline", 0);
        output4[id][2] = Countx(jsonData, "BENEFICIARY_INFO/Status", [[status[id]]], "Midline", 0);
        output4[id][3] = Countx(jsonData, "BENEFICIARY_INFO/Status", [[status[id]]], "Endline", 0);
    }
    output4 = tsort(output4, 1);
    sheetbg.getRange(y, 1, status.length, 4).setValues(output4);
    y = y + status.length + 1;

    sheetbg.getRange(y, 1).setValue("● GENDER");
    sheetbg.getRange(y, 2).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspBaseline");
    sheetbg.getRange(y, 3).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMidline");
    sheetbg.getRange(y, 4).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspEndline"); y++;
    var output5 = [];
    for (ie = 0; ie < gender.length; ie++) {
        output5[ie] = new Array();
        output5[ie][0] = gender[ie];
        output5[ie][1] = Countx(jsonData, "BENEFICIARY_INFO/Gender", [[gender[ie]]], "Baseline", 0);
        output5[ie][2] = Countx(jsonData, "BENEFICIARY_INFO/Gender", [[gender[ie]]], "Midline", 0);
        output5[ie][3] = Countx(jsonData, "BENEFICIARY_INFO/Gender", [[gender[ie]]], "Endline", 0);
    }
    sheetbg.getRange(y, 1, gender.length, 4).setValues(output5);
    y = y + gender.length + 1;

    sheetbg.getRange(y, 1).setValue("● AGE");
    sheetbg.getRange(y, 2).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspBaseline");
    sheetbg.getRange(y, 3).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMidline");
    sheetbg.getRange(y, 4).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspEndline"); y++;
    for (ig = 0; ig < age.length; ig++) {
        sheetbg.getRange(y + ig, 1).setValue(age[ig]);
        if (age[ig] == "Under 18") {
            sheetbg.getRange(y + ig, 2).setValue(agecount1);
            sheetbg.getRange(y + ig, 3).setValue(agecount111);
            sheetbg.getRange(y + ig, 4).setValue(agecount11);
        }
        if (age[ig] == "Over 65") {
            sheetbg.getRange(y + ig, 2).setValue(agecount2);
            sheetbg.getRange(y + ig, 3).setValue(agecount222);
            sheetbg.getRange(y + ig, 4).setValue(agecount22);
        }
        if (age[ig] == "18-65") {
            sheetbg.getRange(y + ig, 2).setValue(agecount3);
            sheetbg.getRange(y + ig, 3).setValue(agecount333);
            sheetbg.getRange(y + ig, 4).setValue(agecount33);
        }
    }
    y = y + age.length + 1;

    sheetbg.getRange(y, 1).setValue("● POC LOCATION");
    sheetbg.getRange(y, 2).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspBaseline");
    sheetbg.getRange(y, 3).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMidline");
    sheetbg.getRange(y, 4).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspEndline"); y++;
    var output3 = [];
    for (ic = 0; ic < camp.length; ic++) {
        output3[ic] = new Array();
        output3[ic][0] = camp[ic];
        output3[ic][1] = Countx(jsonData, "PARTNER_INFO/Camp", [[camp[ic]]], "Baseline", 0);
        output3[ic][2] = Countx(jsonData, "PARTNER_INFO/Camp", [[camp[ic]]], "Midline", 0);
        output3[ic][3] = Countx(jsonData, "PARTNER_INFO/Camp", [[camp[ic]]], "Endline", 0);
    }
    try {
        output3 = tsort(output3, 1);
        sheetbg.getRange(y, 1, camp.length, 4).setValues(output3);
        y = y + camp.length + 1;
    } catch (e) { }

    sheetbg.getRange(y, 1).setValue("● HOST");
    sheetbg.getRange(y, 2).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspBaseline");
    sheetbg.getRange(y, 3).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMidline");
    sheetbg.getRange(y, 4).setValue("&nbsp&nbsp&nbsp&nbsp&nbsp&nbspEndline"); y++;
    var output7 = [];
    for (ij = 0; ij < host.length; ij++) {
        output7[ij] = new Array();
        output7[ij][0] = host[ij];
        output7[ij][1] = Countx(jsonData, "PARTNER_INFO/Nearby_Camp", [[host[ij]]], "Baseline", 0);
        output7[ij][2] = Countx(jsonData, "PARTNER_INFO/Nearby_Camp", [[host[ij]]], "Midline", 0);
        output7[ij][3] = Countx(jsonData, "PARTNER_INFO/Nearby_Camp", [[host[ij]]], "Endline", 0);
    }
    try {
        output7 = tsort(output7, 1);
        sheetbg.getRange(y, 1, host.length, 4).setValues(output7);
        y = y + host.length + 1;
    } catch (e) { }

    var table = sheetbg.getDataRange().getValues();

    /*creates and sends an email (dataid, subject, message0, summarytable, UID).*/

    EMAILSAMPLEPROFILE(dataid, "New Data Submitted", "New data have been submitted", table, UID);
    
    if (pkey != null) { return (keyinputs); }
    return 1;

Function Countx

Countx(item, header, category, line)

The function counts the number of item in a given item, depending on header and category.

Arguments
  • item (array) – array of data

  • header (array) – array of headers columns

  • category (string) – type of category

  • line (string) – Baseline/Midline/Endline

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    var result = 0;
    var count = 0;
    for (i = 0; i < item.length; i++) {

        for (ii = 0; ii < category.length; ii++) {

            if (item[i]["PARTNER_INFO/BE"] != line) { continue; }

            if (category[ii] == "Total" && item[i][header] != "") {
                count++;
                continue;
            }
            if (contain == 0) {
                if (item[i][header] != "" && String(item[i][header]) == category[ii]) {
                    count++;
                    continue;
                }
            }
            if (contain == 1) {
                if (item[i][header] != "" && String(item[i][header]).indexOf(category[ii]) > -1) {
                    count++;
                    continue;
                }
            }
            if (item[i][header] == "0" && category[ii] == "Other") {
                count++;
                continue;
            }
        }
        if (count > 0) {
            result++;
            count = 0;
        } else {
            count = 0;
        }
    }
    return result;
}

Function EMAILSAMPLEPROFILE

EMAILSAMPLEPROFILE(dataid, subject, message, summarytable, UID)

The function creates and sends an email

Arguments
  • dataid (string) – the dataid of form

  • subject (string) – subject of the email

  • message (string) – message of the email

  • summarytable (array) – array of summary table

  • UID (string) – current user

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function EMAILSAMPLEPROFILE(dataid, subject, message0, summarytable, UID) {

    var paramater = ss.getSheetByName("PARAMATERS");
    var emails = paramater.getRange(2, 1).getValue();
    var data = getData("CONTROL");
    var message;

    var d = new Date();
    var dd = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yyyy")

    for (i = 0; i < data.length; i++) {
        if (data[i][data[0].indexOf("_id")] == dataid) {
            var row = i;
            break;
        }
    }


    /*generating message*/
    var draft = "<br><span style='color:black'>Please note that new data have been submitted from your operation. On " + dd + ", the summary profile of all the samples submitted so far is the following.</span>";
    var draft = draft + "<br><br><span style='color:black'>Please kindly confirm when all the data collection and submission are completed, and that all the submitted data have been received correctly at the server.</span>";
    var draft = draft + "<br><br><b><span style='color:navy'>" + summarytable[0][0] + " " + data[row][data[0].indexOf('CONTACT_INFORMATION/Country')] + "</b></span>";
    for (var c = 1; c < summarytable.length; ++c) {
        if (summarytable[c][0] == "") { draft = draft + '<br><br><table style="background-color:white;font-family:Arial"'; }//border-collapse:collapse;" border = 1 cellpadding = 5><th>Cetegory</th><th>Sample</th><tr>';}
        else { draft = draft + '<tr><td>' + summarytable[c][0] + '</td><td align="right">' + summarytable[c][1] + '</td><td align="right">' + summarytable[c][2] + '</td><td align="right">' + summarytable[c][3] + '</td></tr>'; }
        if (c < summarytable.length - 1) { if (summarytable[c + 1][0] == "") { draft = draft + "</table>"; } }
        if (c == summarytable.length - 1) { draft = draft + "</table>"; }
    }


    /*addting data to the message*/
    message = "<span style='color:black'>" + message0 + "</span>";
    message = message + "<br><br><b><span style='color:navy'>1. This notification is related to the following template:</span></b>";
    message = message + "<br><br><b>● Year</b>: " + data[row][data[0].indexOf("PROGRAMME_SYNOPSIS/PROGRAMME_INFORMATION/Year")];
    message = message + "<br><b>● Country</b>: " + data[row][data[0].indexOf("CONTACT_INFORMATION/Country")];
    message = message + "<br><b>● Duty Station</b>: " + data[row][data[0].indexOf("CONTACT_INFORMATION/D_Station")];
    message = message + "<br><br><b>● Focal Point</b>: " + data[row][data[0].indexOf("CONTACT_INFORMATION/F_Point")];
    message = message + "<br><b>● E-Mail</b>: " + data[row][data[0].indexOf("CONTACT_INFORMATION/Email_FP")];

    if (draft != null && draft != 0) {
        message = message + "<br><br><b><span style='color:navy'>2. Draft email to the focal point:</span></b>";
        message = message + "<br><br>Dear " + data[row][data[0].indexOf("CONTACT_INFORMATION/F_Point")];
        message = message + "<br>" + draft;
        
    }

    /*adding mainfolder link*/
    var mainfolderid = createFolder(0, key["MAINFOLDER"]);
    var newurl = DriveApp.getFolderById(mainfolderid).getUrl();
    var newurl2 = DriveApp.getFileById(thisid).getUrl();
    message = message + "<br><br><b><span style='color:navy'>3. For further information:</span></b>";
    message = message + "<br><br><b>Main folder</b>: " + newurl
    message = message + "<br><b>Control Panel</b>: " + newurl2

    var html = '<span style="color:black; font-size:90%;font-family:Arial">' + message + '</span>'

    /* send email */
    try {
        MailApp.sendEmail("", subject, message, {
            name: key["SECTOR"] + ' Monitoring System',
            bcc: emails,
            htmlBody: html
        });
    } catch (e) { }

    return 1;
}