Manual Update GAS SCRIPT

Function UPDATEUSERNAME [ManualUpdate.gs]

Function calls UPDATEUSERNAME function [User managment Component]

1
2
3
function UPDATEUSERNAME() {
    Keys.setProperties(LM.UPDATEUSERNAME(null, key));
}

Function UPDATADIALOG

UPDATADIALOG()

The function opens a dialog box ‘Update B-Data Sheet’ in the user’s editor

1
2
3
4
5
6
function UPDATADIALOG() {
    var paramater = ss.getSheetByName("GV");
    paramater.getRange(1, 1).setValue("UPDATA");
    var html = HtmlService.createHtmlOutputFromFile('TEMPDIALOG').setHeight(290).setWidth(520);
    SpreadsheetApp.getUi().showModalDialog(html, 'UPDATE DATASHEET');
}

Function CONTEXTDIALOG

CONTEXTDIALOG()

The function opens a dialog box ‘Updating the problem statement’ in the user’s editor

1
2
3
4
function CONTEXTDIALOG() {
    var html = HtmlService.createHtmlOutputFromFile('CONTEXTDIALOG').setHeight(550).setWidth(720);
    SpreadsheetApp.getUi().showModalDialog(html, 'UPDATING THE PROBLEM STATEMENT');
}

Function SAMPLEPROFILEDIALOG

SAMPLEPROFILEDIALOG()

The function opens a dialog box ‘Emailing sample data profile’ in the user’s editor

1
2
3
4
5
6
function SAMPLEPROFILEDIALOG() {
    var paramater = ss.getSheetByName("GV");
    paramater.getRange(1, 1).setValue("SAMPLE");
    var html = HtmlService.createHtmlOutputFromFile('TEMPDIALOG').setHeight(290).setWidth(520);
    SpreadsheetApp.getUi().showModalDialog(html, 'EMAILING SAMPLE DATA PROFILE');
}

Function LOCATIONDIALOG

LOCATIONDIALOG()

The function opens a dialog box ‘Update existing location CSV file’ in the user’s editor

1
2
3
4
5
6
function LOCATIONDIALOG() {
    var paramater = ss.getSheetByName("GV");
    paramater.getRange(1, 1).setValue("LOCATION");
    var html = HtmlService.createHtmlOutputFromFile('TEMPDIALOG').setHeight(290).setWidth(520);
    SpreadsheetApp.getUi().showModalDialog(html, 'UPDATE EXISTING LOCATION CSV FILE');
}

Function FORMDIALOG

FORMDIALOG()

The function opens a dialog box ‘Update existing Kobo forms (Not Recommended)’ in the user’s editor

1
2
3
4
function FORMDIALOG() {
    var html = HtmlService.createHtmlOutputFromFile('FORMDIALOG').setHeight(200).setWidth(520);
    SpreadsheetApp.getUi().showModalDialog(html, 'UPDATE EXISTING KOBO FORMS (NOT RECOMMENDED)');
}

Function TEMPDIALOG

TEMPDIALOG()

The function opens a dialog box ‘Extract template’ in the user’s editor

1
2
3
4
5
6
function TEMPDIALOG() {
    var paramater = ss.getSheetByName("GV");
    paramater.getRange(1, 1).setValue("TEMPLATE");
    var html = HtmlService.createHtmlOutputFromFile('TEMPDIALOG').setHeight(290).setWidth(520);
    SpreadsheetApp.getUi().showModalDialog(html, 'EXTACT TEMPLATE');
}

Function CNOTEDIALOG

CNOTEDIALOG()

The function opens a dialog box ‘Extract Country Analysis Note’ in the user’s editor

1
2
3
4
5
6
function CNOTEDIALOG() {
    var paramater = ss.getSheetByName("GV");
    paramater.getRange(1, 1).setValue("CNOTE");
    var html = HtmlService.createHtmlOutputFromFile('TEMPDIALOG').setHeight(290).setWidth(520);
    SpreadsheetApp.getUi().showModalDialog(html, 'EXTACT COUNTRY ANALYSIS NOTE');
}

Function CSVDIALOG

CSVDIALOG()

The function opens a dialog box ‘Customise/Update Csv files’ in the user’s editor

1
2
3
4
5
6
function CSVDIALOG() {
    var paramater = ss.getSheetByName("GV");
    paramater.getRange(1, 1).setValue("CSV");
    var html = HtmlService.createHtmlOutputFromFile('TEMPDIALOG').setHeight(290).setWidth(520);
    SpreadsheetApp.getUi().showModalDialog(html, 'CUSTOMISE / UPDATE CSV FILES');
}

Function USERDIALOG

USERDIALOG()

The function opens a dialog box ‘User Access Control’ in the user’s editor

1
2
3
4
5
6
function USERDIALOG() {
    var paramater = ss.getSheetByName("GV");
    paramater.getRange(1, 1).setValue("USER");
    var html = HtmlService.createHtmlOutputFromFile('TEMPDIALOG').setHeight(290).setWidth(520);
    SpreadsheetApp.getUi().showModalDialog(html, 'USER ACCESS CONTROL');
}

Function changerow

changerow(rrr, m)

The Function interacts with html dialog (CONTROL)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function changerow(rrr, m) {

    var nowsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

    if (rrr == null) { var rrr = nowsheet.getActiveCell().getRow() - 1; if (rrr < 2) { rrr = 2; } };
    if (m == 1) { if (rrr == 2) { rrr = 2; } else { rrr--; } } else if (m == 0) { rrr++; }

    var data = nowsheet.getRange(1, 1, nowsheet.getLastRow(), nowsheet.getLastColumn()).getValues();
    var C1 = data[0].indexOf("PROGRAMME_SYNOPSIS/PROGRAMME_INFORMATION/Year");
    var C2 = data[0].indexOf("formid");
    var C3 = data[0].indexOf("_id");
    var C4 = data[0].indexOf("CONTACT_INFORMATION/Country");
    var C5 = data[0].indexOf("CONTACT_INFORMATION/D_Station");
    var C6 = data[0].indexOf("Username");
    var C7 = data[0].indexOf("_uuid");
    var response = [data[rrr][C1], data[rrr][C2], data[rrr][C3], data[rrr][C4], data[rrr][C5], data[rrr][C6], data[rrr][C7], rrr];
    rrr--;
    return response;
}

Function changerow2

changerow2(rrr, m)

The Function interacts with html dialog (BSURVEY UPDATE)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function changerow2(rrr, m) {

    var nowsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

    if (rrr == null) { var rrr = nowsheet.getActiveCell().getRow() - 1; if (rrr < 1) { rrr = 1; } };
    if (m == 1) { if (rrr == 1) { rrr = 1; } else { rrr--; } } else if (m == 0) { rrr++; }
    var data = nowsheet.getRange(1, 1, nowsheet.getLastRow(), nowsheet.getLastColumn()).getValues();
    var C1 = data[0].indexOf("YEAR");
    var C2 = data[0].indexOf("title");
    var C3 = data[0].indexOf("formid");
    var response = [data[rrr][C1], data[rrr][C2], data[rrr][C3], rrr];
    rrr--;
    return response;
}

Function changerow3

changerow3(rrr, m)

The Function interacts with context dialog (ANALYSISSHEET UPDATE)

 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
function changerow3(rrr, m) {
   
    var nowsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

    if (rrr == null) { var rrr = nowsheet.getActiveCell().getRow() - 1; if (rrr < 1) { rrr = 1; } };
    if (m == 1) { if (rrr == 1) { rrr = 1; } else { rrr--; } } else if (m == 0) { rrr++; }
    var data = nowsheet.getRange(1, 1, nowsheet.getLastRow(), nowsheet.getLastColumn()).getValues();
    var C1 = data[0].indexOf("PROGRAMME_SYNOPSIS/PROGRAMME_INFORMATION/Year");
    var C2 = data[0].indexOf("formid");
    var C3 = data[0].indexOf("_id");
    var C4 = data[0].indexOf("CONTACT_INFORMATION/Country");
    var C5 = data[0].indexOf("CONTACT_INFORMATION/D_Station");
    var C6 = data[0].indexOf("Username");
    var C7 = data[0].indexOf("_uuid");
  


    var year = data[rrr][C1];
    //TEXT
    var url = key["CSVURL4" + data[rrr][C3]];
   // if (url == null) { Browser.msgBox("ERROR: The analysis sheet has not been created yet."); return; }
    try{
    var idSS = url.replace('https://docs.google.com/spreadsheets/d/', '');
    idSS = idSS.replace('/edit?usp=drivesdk', '');

    var anss = SpreadsheetApp.openById(idSS);
    var context = anss.getSheetByName("Context");
    var textall = context.getDataRange().getValues();

    var u = data[rrr][C6];
    var text;

    for (fc = 0; fc < textall.length; fc++) {
        if (textall[fc][1] == u) { text = textall[fc][2]; break; }
    }

}catch(e){}
    var response = [data[rrr][C1], data[rrr][C2], data[rrr][C3], data[rrr][C4], data[rrr][C5], data[rrr][C6], data[rrr][C7], rrr, text];
    rrr--;

Function TEMPSUBMIT2

TEMPSUBMIT2(title, year)

The Function sets the properties for updating form and shows the message box. It starts when Submit button is selected (FORM)

Arguments
  • title (string) – name of the form

  • year (string) – selected year

  • title – template id

1
2
3
4
function TEMPSUBMIT2(title, year, formid) {
    Keys.setProperties(LM.UpdateFormCreateBlob(title, year, formid, key));
    Browser.msgBox("Task finished for " + title + " " + year);
}

Function TEMPSUBMIT3

TEMPSUBMIT3(year, country, username, text, dataid)

The Function starts when Submit button is selected (Narrative update)

Arguments
  • year (string) – selected year

  • country (string) – selected country

  • username (string) – selected username

  • text (string) – text for display

  • dataid (string) – id of the data (BENEFICIARY/PARTER survey)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function TEMPSUBMIT3(year, country, username, text, dataid) {    
    var url = key["CSVURL4" + dataid];
    if (url == null) { Browser.msgBox("ERROR: The analysis sheet has not been created yet."); return; }
    if (url == null) { return; }
    var idSS = url.replace('https://docs.google.com/spreadsheets/d/', '');
    idSS = idSS.replace('/edit?usp=drivesdk', '');
    var anss = SpreadsheetApp.openById(idSS);
    var context = anss.getSheetByName("Context");
    var textall = context.getDataRange().getValues();

    var count = 0;
    for (fc = 0; fc < textall.length; fc++) {
        if (textall[fc][1] == username) { textall[fc][2] = text; count++; break; }
    }

    if (count > 0) { context.getDataRange().setValues(textall); }
    if (count == 0) { context.appendRow([country, username, text]); }
    keyinputs["COUNTRYNOTE" + dataid] = "UPDATE"; key["COUNTRYNOTE" + dataid] = "UPDATE";
    keyinputs["CNOTE" + dataid] = ""; key["CNOTE" + dataid] = "";

    Keys.setProperties(LM.updateOnCustomisation(dataid, key));
    CNOTEDIALOG();
}

Function TEMPSUBMIT

TEMPSUBMIT(year, formid, dataid, country, dstation, username, taskmode, uuid)

The Function starts when Submit button is selected for different form (CONTROL)

Arguments
  • year (string) – selected year

  • formid (string) – selected template id

  • dataid (string) – id of the data (BENEFICIARY/PARTER survey)

  • country (string) – selected country

  • dstation (string) – selected location

  • username (string) – selected username

  • taskmode (string) – NOW/SCHEDULE

  • uuid (string) – current username

  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
function TEMPSUBMIT(year, formid, dataid, country, dstation, username, taskmode, uuid) {
    var paramater = ss.getSheetByName("GV");
    var EXMODE = paramater.getRange(1, 1).getValue();

    var filename = country + "_" + dstation;

    /*Update datasheet*/
    {//UPDATE DATASHEET
        if (EXMODE == "UPDATA") {
            /*calls the function from library [GENERAL COMPONENT]*/
            var tdata = LM.getData("CONTROL");
            for (ii = 0; ii < tdata.length; ii++) {
                if (tdata[ii][tdata[0].indexOf("_id")] == dataid && tdata[ii][tdata[0].indexOf("Baseline")] == "" && tdata[ii][tdata[0].indexOf("Endline")] == "") {
                    Browser.msgBox("No data is available for " + country + " " + year + " " + dataid);
                    return;
                }
            }
           /*calls the function from library [GENERAL COMPONENT]*/
            var bdata = LM.getData("BSURVEY");
            for (i = 0; i < bdata.length; i++) {
                if (bdata[i][bdata[0].indexOf("YEAR")] == year && bdata[i][bdata[0].indexOf("title")].indexOf("DEMO") == -1 && bdata[i][bdata[0].indexOf("title")].indexOf("Partner") == -1) {
                    var bsurveyid = bdata[i][bdata[0].indexOf("formid")];
                }
            }

            if (taskmode == "NOW") {
                Browser.msgBox("Task will be excuted now for " + country + " " + year);
                /*calls the function from library [DATA ANALYSIS COMPONENT]*/ 
                Keys.setProperties(LM.CREATEANALYSISSHEET(year,
                    bsurveyid,
                    formid,
                    country,
                    dataid, key));

                Browser.msgBox("Task finished for " + country + " " + year);
            }

            if (taskmode == "SCHEDULE") {

                LM.SCHEDULE("CREATEANALYSISSHEET", year,
                    bsurveyid,
                    formid,
                    country,
                    dataid);

        
                Browser.msgBox("Task successfully scheduled for " + country + " " + year);
            }
        }
    }

   /*Template*/
    {
        if (EXMODE == "TEMPLATE") {
            Keys.setProperty("TEMPLATE" + dataid, "");
            if (taskmode == "NOW") {
                Browser.msgBox("Task will be excuted now for " + country + " " + year);
                /*calls the function from library [TEMPLATE COMPONENT]*/
                Keys.setProperties(LM.scheduleCreatePdfReport(year,
                    formid,
                    dataid,
                    filename, key));
                Browser.msgBox("Task finished for " + country + " " + year);
            }

            if (taskmode == "SCHEDULE") {
                 /*calls the function from library [GENERAL COMPONENT]*/
                LM.SCHEDULE("TEMPLATEREPORT", year,
                    formid,
                    dataid,
                    filename);
                
                Browser.msgBox("Task successfully scheduled for " + country + " " + year);
            }
        }
    }

    /*Country analysis note*/
    {
        if (EXMODE == "CNOTE") {
            Keys.setProperty("COUNTRYNOTE" + dataid, "");
            var verify = LM.getData("CONTROL");
            for (i = 0; i < verify.length; i++) {
                if (verify[i][verify[0].indexOf("_id")] == dataid) {

                    if (verify[i][verify[0].indexOf("CONTACT_INFORMATION/Country")].toLowerCase() != verify[i][verify[0].indexOf("Username")]) {
                        var username = verify[i][verify[0].indexOf("Username")];
                        var dutystation = verify[i][verify[0].indexOf("CONTACT_INFORMATION/D_Station")];
                    }
                    else {
                        var username = verify[i][verify[0].indexOf("Username")];
                        var dutystation = verify[i][verify[0].indexOf("CONTACT_INFORMATION/D_Station")];
                    }

                    if (verify[i][verify[0].indexOf("Baseline")] == "" && verify[i][verify[0].indexOf("Endline")] == "") {
                        Browser.msgBox("There is no data available to analyse yet");
                        return;
                    }
                    break;
                }
            }



            if (taskmode == "NOW") {
                Browser.msgBox("Task will be excuted now for " + country + " " + year);
                  /*calls the function from library [DATA ANALYSIS COMPONENT]*/
                Keys.setProperties(LM.extractAnalysisNote(year, country, username, dstation, dataid, key));
                Browser.msgBox("Task finished for " + country + " " + year);
            }

            if (taskmode == "SCHEDULE") {
                /*calls the function from library [DATA ANALYSIS COMPONENT]*/
                LM.SCHEDULE("ANALYSISNOTEEXTRACT", year,
                    country,
                    username,
                    dstation,
                    dataid);
                 Browser.msgBox("Task successfully scheduled for " + country + " " + year);
            }
        }
    }
    /*CSV*/
    {
        if (EXMODE == "CSV") {
            var kk = 'UN' + dataid;
            var kko = 'UNO' + dataid;
            Keys.setProperty("CUSTOMISE" + dataid, "");
            if (taskmode == "NOW") {
                Browser.msgBox("Task will be excuted now for " + country + " " + year);
                Keys.setProperties(LM.createCustomiseCSV(year, formid, dataid, uuid, key[kko], key[kk], key));
                Browser.msgBox("Task finished for " + country + " " + year);
            }

            if (taskmode == "SCHEDULE") {
                LM.SCHEDULE("CUSTOMISE", year, formid, dataid, uuid, key[kko], key[kk]);
                Browser.msgBox("Task successfully scheduled for " + country + " " + year);
            }
        }
    }

    /*Location CSV*/
    {
        if (EXMODE == "LOCATION") {
            var csv3 = "CSVURL3" + year;
            if (taskmode == "NOW") {
                Browser.msgBox("Task will be excuted now for " + year);
                Keys.setProperties(LM.updateManuallyLocationFile(year, key));
                if (result != 2) { Browser.msgBox("Task finished for " + year); }
            }

            if (taskmode == "SCHEDULE") {
                LM.SCHEDULE("LOCATIONUPDATE", year);
                Browser.msgBox("Task successfully scheduled for " + year);
            }
        }
    }
    /*user*/
    {
        if (EXMODE == "USER") {

            var kk = 'UN' + dataid;
            var kko = 'UNO' + dataid;
            var k = 'TEMP' + dataid;
            var kkk = 'UNA' + dataid;

            if (key[kko] != key[kk] || key[k] == "Updating") { Browser.msgBox("CUSTOMISATION MUST BE COMPLETED FIRST. PLEASE REVIEW"); return; }

            Keys.setProperties(LM.updateOnCustomisation(dataid, key));

            var htmlOutput = HtmlService
                .createHtmlOutput('<style>p { margin-left: 0pt; margin-top: 5pt;margin-bottom: 5pt;color: black; font: 16px bold, Arial; }</style><p>The user access information for the selected template has been updated on Control Panel. <br><br>To change the access permission, please do this manually on <a href="https://kobo.unhcr.org/" target="_blank">KoboToolbox</a> for each of the 3 surveys.</p>')
                .setWidth(500) //optional
                .setHeight(100); //optional
            SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'User Access Management');
            return;

        }
    }

    {/*SAMPLE*/
        if (EXMODE == "SAMPLE") {
            if (taskmode == "NOW") {
                Browser.msgBox("Task will be excuted now for " + country + " " + year);
                Keys.setProperties(LM.SAMPLEPROFILE(year, username, dataid, key));
                Browser.msgBox("Task finished for " + country + " " + year);
            }

            if (taskmode == "SCHEDULE") {
                LM.SCHEDULE("SAMPLEPROFILE", year, username, dataid);
                Browser.msgBox("Task successfully scheduled for " + country + " " + year);
            }
        }
    }

}

Function extractProblemStatement

extractProblemStatement(formid)

The Function automatically extracts a problem statement

Arguments
  • formid (string) – selected template 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
function extractProblemStatement(formid, id) {
    Logger.log(key["APITOKEN"]+formid+" "+id);
    var data = LM.connectKobotoolbox(key["APITOKEN"], formid, id);
    if (data["VERSION"] == null) {
        Browser.msgBox("Automated problem statement cannot be extracted beacause the data do not exist.")
        return;
    }
       //#region 1st Paragraph
       var text = "Since <?arrivalyear?> began receiving refugees from <?coo?>. In <?year?>, approximately <?pocpop?> resided in <?location?> and the surrounding local population is estimated at <?hostpop?> individuals living in <?urbanrural?>. The <?year2?> Age, Gender, Diversity Participatory Assessment with the beneficiaries, conducted in all major hosting sites as part of the standard planning process, has identified the priority livelihoods problems faced by the beneficiary population. The key obstacles for the beneficiaries to conduct livelihoods activities are  <?problems?>. Additional problems include <?problems2?>. The participatory assessment has also identified the capacities of the beneficiaries and their communities to <?solutions?>, in order to assure their participation in potential projects adressing these challenges."

       //Year
       {
           text = text.replace("<?year?>", data["PROGRAMME_SYNOPSIS/PROGRAMME_INFORMATION/Year"]);
       }

       var POC;
       //PoC Population
       {
           var replace = data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_poc"].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
           if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "1") { replace = replace + " refugees";  POC = "refugees";}
           if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "2") { replace = replace + " former refugees"; POC = "former refugees"; }
           if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "3") { replace = replace + " refugee returnees"; POC = "refugee returnees"; }
           if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "4") { replace = replace + " IDPs"; POC = "IDPs"; }
           if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "5") { replace = replace + " persons at risk of statelessness"; POC = "persons at risk of statelessness";}
           text = text.replace("<?pocpop?>", replace);
           POC = replace.splitCells(" ");
           if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "3" || data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "4" || data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "5") { text = text.replace("Since <?arrivalyear?> began receiving refugees from <?coo?>.", ""); }
       }

       //County of Origin
       {
           if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "1" || data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_type"] == "2") {
               var replace;
               replace = data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_coo1"].replace(/_/g, " ");
               if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_coo2"] != "NA") { replace = replace + ", " + data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_coo2"].replace(/_/g, " "); }
               if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_coo3"] != null) { if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_coo3"] != "NA") { replace = replace + ", " + data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_coo3"].replace(/_/g, " "); } }
               if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_coo4"] != null) { if (data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_coo4"] == "1") { replace = "various countries, mainly from " + replace; } }
               replace = replace.replace(/,([^,]*)$/, ' and ' + "$1");
               text = text.replace("<?coo?>", replace);
           }
       }

       //Arrival year
       {
           var replace = data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_arrival"];
           replace = replace + " " + data["CONTACT_INFORMATION/Country"];
           text = text.replace("<?arrivalyear?>", replace);

       }

       var LOCATION;
       //Location
       {
            var replace;
            if (data["PROBLEM_STATEMENT/LOCATION/v_scope"] == 1) { replace = data["PROBLEM_STATEMENT/LOCATION/v_one_location"].replace(/,/g,", ").replace(/,\s\s/g,", ") + " in the " + data["PROBLEM_STATEMENT/LOCATION/v_one_region_location"] + " " + data["CONTACT_INFORMATION/Country"].replace("the ",""); }
            else if (data["PROBLEM_STATEMENT/LOCATION/v_scope"] == 2) { replace = data["PROBLEM_STATEMENT/LOCATION/v_one_region"].replace(/,/g,", ").replace(/,\s\s/g,", ") + " in the" + data["PROBLEM_STATEMENT/LOCATION/v_one_region_location"] + " " + data["CONTACT_INFORMATION/Country"].replace("the ",""); }
            else if (data["PROBLEM_STATEMENT/LOCATION/v_scope"] == 3) { replace = data["PROBLEM_STATEMENT/LOCATION/v_one_region_location"] + " " + data["CONTACT_INFORMATION/Country"]; }
            text = text.replace("<?location?>", replace);
            LOCATION = replace;
       }

       //Host community
       {
          var replace = data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_host"].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
         
          text = text.replace("<?hostpop?>", replace);
       }

       //UrbanRural
       {
           var replace = "a primarily " + data["PROBLEM_STATEMENT/LOCATION/v_one_urbanrural"].replace(/_/g, " ") + " setting"
           if (data["PROBLEM_STATEMENT/LOCATION/v_one_urbanrural"].indexOf("both") > -1) { replace = replace.replace("both ", ""); }
           text = text.replace("<?urbanrural?>", replace);

       }
       // //CONTEXT
       // {
       //     var replace = data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_situation"].replace(/_/g, " ") + ", where " + POC[1] + " are " + data["PROBLEM_STATEMENT/BENEFICIARYPROFILE/v_dsolution"].replace(/_/g, " ");
       //     text = text.replace("<?CONTEXT?>", replace);
       // }
       //AGDM Year
       {
           var replace = data["PROBLEM_STATEMENT/AGDMV/v_agdm_year"];
           text = text.replace("<?year2?>", replace);
       }

       //AGDM Problems A1
       {
           //if (data["INDICATOR_SELECTION/O1"] != 0) {
           var replace = "1) " + data["PROBLEM_STATEMENT/AGDMV/v_priority1_a1_problem"].replace(/_/g, " ");
           replace = replace + ", 2) " + data["PROBLEM_STATEMENT/AGDMV/v_priority2_a1_problem"].replace(/_/g, " ");
           replace = replace + ", and 3) " + data["PROBLEM_STATEMENT/AGDMV/v_priority3_a1_problem"].replace(/_/g, " ");
           text = text.replace("<?problems?>", replace);
           // } else { text = text.replace("The problems due to the lack of apprioriate access to fuel are <?problems?>. ", ""); }
       }

       //AGDM Problems A2
       {
           //if (data["INDICATOR_SELECTION/O2"] != 0) {
            var replace = data["PROBLEM_STATEMENT/AGDMV/v_priority1_a2_problem"].replace(/_/g, " ").toLowerCase();
            replace = replace + ", " + data["PROBLEM_STATEMENT/AGDMV/v_priority2_a2_problem"].replace(/_/g, " ").toLowerCase();
            replace = replace + ", and " + data["PROBLEM_STATEMENT/AGDMV/v_priority3_a2_problem"].replace(/_/g, " ").toLowerCase();
           // } else { text = text.replace("The main problems due to lack of appropriate access to lighting are <?problems2?>. ", ""); }
       }

       //AGDM Solutions
       {
           var replace = "";
           var R = data["PROBLEM_STATEMENT/AGDMV/v_solutions_a1"].splitCells(" ");
           if (R.length == 1) { replace = data["PROBLEM_STATEMENT/AGDMV/v_solutions_a1"].replace(/_/g, " "); }
           else {
               for (var i = 0; i < R.length; i++) {
                   if (R.length == 2 && i == 1) { replace = replace + " and "; }
                   else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
                   else if (i > 0) { replace = replace + ", "; }
                   var ii = i + 1;
                   replace = replace + ii.toString()+") "+R[i].replace(/_/g, " ");
               }
           }
           text = text.replace("<?solutions?>", replace);
       }

       //#endregion

       //#region 2nd Paragraph

       //String.fromCharCode(10) + String.fromCharCode(10) +
       text = text + "\\n\\nTo inform an evidence-based program design, livelihoods related assessments have been conducted in <?assessmentyear?>. The purpose of the assessments was to evaluate / conduct <?purpose?> through <?methodology?>. <?POVERTY?> <?ANALYSIS?>"

       //Year
       {
           var replace = "";
           var R = data["PROBLEM_STATEMENT/TECHNICALV/v_assessment_year_a1"].splitCells(" ");
           for (var i = 0; i < R.length; i++) {
               if (R.length == 2 && i == 1) { replace = replace + " and "; }
               else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
               else if (i > 0) { replace = replace + ", "; }
               var ii = i + 1;
               replace = replace + R[i].replace(/_/g, " ");
           }
           text = text.replace("<?assessmentyear?>", replace);
       }

       //Purpose
       {
           var replace = "";
           var R = data["PROBLEM_STATEMENT/TECHNICALV/v_purpose_a1"].splitCells(" ");
           if (R.length == 1) { replace = data["PROBLEM_STATEMENT/TECHNICALV/v_purpose_a1"].replace(/_/g, " "); }
           else {
               for (var i = 0; i < R.length; i++) {
                   if (R.length == 2 && i == 1) { replace = replace + " and "; }
                   else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
                   else if (i > 0) { replace = replace + ", "; }
                   var ii = i + 1;
                   replace = replace + R[i].replace(/_/g, " ");
               }
           }

           text = text.replace("<?purpose?>", replace);
       }

       //Methodology
       {
           var replace = "";
           var R = data["PROBLEM_STATEMENT/TECHNICALV/v_methodology_a1"].splitCells(" ");
           if (R.length == 1) { replace = data["PROBLEM_STATEMENT/TECHNICALV/v_methodology_a1"].replace(/_/g, " "); }
           else {
               for (var i = 0; i < R.length; i++) {
                   if (R.length == 2 && i == 1) { replace = replace + " and "; }
                   else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
                   else if (i > 0) { replace = replace + ", "; }
                   var ii = i + 1;
                   replace = replace + R[i].replace(/_/g, " ");
               }
           }
           text = text.replace("<?methodology?>", replace);
       }

       //POVERTY
       {
           if (data["PROBLEM_STATEMENT/TECHNICALV/v_purpose_a1"].indexOf("poverty") > -1) {
               text = text.replace("<?POVERTY?>", "The assessment analysed <?typeanalysis?>, and found that approximately <?%poverty?> of the beneficiary population are living under poverty. <?povertyhost?> <?tention?>");

               //Poverty %
               if (data["PROBLEM_STATEMENT/TECHNICALV/v_percent_poor"] != "0") {
                   text = text.replace("<?%poverty?>", data["PROBLEM_STATEMENT/TECHNICALV/v_percent_poor"]);
               }
               else {
                   text = text.replace(", and found that approximately <?%poverty?> of the beneficiary population are living under poverty", "");
               }
               //Analysis
               {
                   var replace = "";
                   var R = data["PROBLEM_STATEMENT/TECHNICALV/v_type_poor"].splitCells(" ");
                   if (R.length == 1) { replace = data["PROBLEM_STATEMENT/TECHNICALV/v_type_poor"].replace(/_/g, " ");; }
                   else {
                       for (var i = 0; i < R.length; i++) {
                           if (R.length == 2 && i == 1) { replace = replace + " and "; }
                           else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
                           else if (i > 0) { replace = replace + ", "; }
                           var ii = i + 1;
                           replace = replace + R[i].replace(/_/g, " ");
                       }
                   }
                   text = text.replace("<?typeanalysis?>", replace);
               }

               //HOST COMPARISON
               if (data["PROBLEM_STATEMENT/TECHNICALV/v_host_a1"] != "0") {
                   text = text.replace("<?povertyhost?>", "In the host communities, the living condition is generally " + data["PROBLEM_STATEMENT/TECHNICALV/v_host_a1"].replace(/_/g, " ") + " compared to that of the " + POC[1] + ".");
               }
               else {
                   text = text.replace("<?povertyhost?> ", "");
               }

               {
                   if (data["PROBLEM_STATEMENT/TECHNICALV/v_competition"] == "Yes") {
                       text = text.replace("<?tention?>", "The presence of " + POC[1] + " can increase the competition with the host population over the limited livelihoods assets and opportunities.");
                   } else { text = text.replace("<?tention?>", ""); }
               }

           } else { text = text.replace("<?POVERTY?>", ""); }
       }

       //SECTORS, CHALLENGES & OPPORTUNITIES
       {

           text = text.replace("<?ANALYSIS?>", "The assessment(s) found that the best potentials for the beneficiary population exist in the areas of <?SECTOR?>. The key challenges identified for the beneficiaries are their limited access to <?CHALLENGE?>, while the main opportunities are the potential/existing access to <?OPPORTUNITIES?>. The assessment(s) recommended UNHCR and its partners to <?INTERVENTIONS?>.");

           //        //SECTORWHY
           //        {
           //            var replace = "";
           //            var R = data["PROBLEM_STATEMENT/TECHNICALV/v_sector_why"].splitCells(" ");
           //            if (R.length == 1) { replace = data["PROBLEM_STATEMENT/TECHNICALV/v_sector_why"].replace(/_/g, " ");; }
           //            else {
           //                for (var i = 0; i < R.length; i++) {
           //                    if (R.length == 2 && i == 1) { replace = replace + " and "; }
           //                    else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
           //                    else if (i > 0) { replace = replace + ", "; }
           //                    var ii = i + 1;
           //                    replace = replace + R[i].replace(/_/g, " ");
           //                }
           //            }
           //            text = text.replace("<?SECTORWHY?>", replace);
           //        }

           //SECTOR
           {
               var replace = "";
               var R = data["PROBLEM_STATEMENT/TECHNICALV/v_sector"].toLowerCase().splitCells(" ");
               if (R.length == 1) { replace = data["PROBLEM_STATEMENT/TECHNICALV/v_sector"].replace(/_/g, " "); }
               else {
                   for (var i = 0; i < R.length; i++) {
                       if (R.length == 2 && i == 1) { replace = replace + " and "; }
                       else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
                       else if (i > 0) { replace = replace + ", "; }
                       var ii = i + 1;
                       replace = replace + R[i].replace(/_/g, " ");
                   }
               }
               text = text.replace("<?SECTOR?>", replace);
           }

           //CHALLENGES
           {
               var replace = "";
               var R = data["PROBLEM_STATEMENT/TECHNICALV/v_challenge"].splitCells(" ");
               if (R.length == 1) { replace = data["PROBLEM_STATEMENT/TECHNICALV/v_challenge"].replace(/_/g, " ").toLowerCase(); }
               else {
                   for (var i = 0; i < R.length; i++) {
                       if (R.length == 2 && i == 1) { replace = replace + " and "; }
                       else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
                       else if (i > 0) { replace = replace + ", "; }
                       var ii = i + 1;
                       replace = replace + R[i].replace(/_/g, " ");
                   }
               }
               text = text.replace("<?CHALLENGE?>", replace);
           }

           //OPPORTUNITIES
           {
               var replace = "";
               var R = data["PROBLEM_STATEMENT/TECHNICALV/v_opportunities"].splitCells(" ");
               if (R.length == 1) { replace = data["PROBLEM_STATEMENT/TECHNICALV/v_opportunities"].replace(/_/g, " ").toLowerCase(); }
               else {
                   for (var i = 0; i < R.length; i++) {
                       if (R.length == 2 && i == 1) { replace = replace + " and "; }
                       else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
                       else if (i > 0) { replace = replace + ", "; }
                       var ii = i + 1;
                       replace = replace + R[i].replace(/_/g, " ");
                   }
               }
               text = text.replace("<?OPPORTUNITIES?>", replace);
           }

           //INTERVENTIONS
           {
               var replace = "";
               var R = data["PROBLEM_STATEMENT/TECHNICALV/v_interventions"].splitCells(" ");
               if (R.length == 1) { replace = data["PROBLEM_STATEMENT/TECHNICALV/v_interventions"].replace(/_/g, " ").toLowerCase(); }
               else {
                   for (var i = 0; i < R.length; i++) {
                       if (R.length == 2 && i == 1) { replace = replace + " and "; }
                       else if (i != 0 && i == R.length - 1) { replace = replace + ", and "; }
                       else if (i > 0) { replace = replace + ", "; }
                       var ii = i + 1;
                       replace = replace + R[i].replace(/_/g, " ");
                   }
               }
               text = text.replace("<?INTERVENTIONS?>", replace);
           }
       }

       //#endregion

       //#region 3rd Paragraph

       //PROGRAMME AREAS STRATEGY
       {
           text = text + "\\n\\nAs a result of the participatory and technical assessments outlined above, the UNHCR Livelihoods Programme for " + POC[1] + " in the " + LOCATION + " (<?strategyyear?>) aims to promote <?STRATEGY?>. In <?year?>, UNHCR and its partners implement project(s) to provide <?AREA1?> <?AREA2?>"
           text = text.replace("<?year?>", data["PROGRAMME_SYNOPSIS/PROGRAMME_INFORMATION/Year"]);

           var replace = "";
           if (data["INDICATOR_SELECTION/O1"] != null && data["INDICATOR_SELECTION/O2"] != null && data["INDICATOR_SELECTION/O3"] != null) {
               replace = "agriculture production, self-employment and wage-employment in non-agriculture sectors";
           }
           else if (data["INDICATOR_SELECTION/O1"] != null && data["INDICATOR_SELECTION/O2"] != null && data["INDICATOR_SELECTION/O3"] == null) {
               replace = "agriculture production and self-employment in non-agriculture sectors";
           }
           else if (data["INDICATOR_SELECTION/O1"] != null && data["INDICATOR_SELECTION/O2"] == null && data["INDICATOR_SELECTION/O3"] != null) {
               replace = "agriculture production and wage-employment in non-agriculture sectors";
           }
           else if (data["INDICATOR_SELECTION/O1"] == null && data["INDICATOR_SELECTION/O2"] != null && data["INDICATOR_SELECTION/O3"] != null) {
               replace = " self-employment and wage-employment in non-agriculture sectors";
           }
           else if (data["INDICATOR_SELECTION/O1"] != null) {
               replace = "agriculture production";
           }
           else if (data["INDICATOR_SELECTION/O2"] != null) {
               replace = "self-employment in non-agriculture sectors";
           }
           else if (data["INDICATOR_SELECTION/O3"] != null) {
               replace = "wage-employment in non-agriculture sectors";
           }
           text = text.replace("<?strategyyear?>", (data["PROBLEM_STATEMENT/STRATEGY/v_startyear"] + "-" + data["PROBLEM_STATEMENT/STRATEGY/v_endyear"]));
           text = text.replace("<?STRATEGY?>", replace);
       }

       var tbene = countBeneficiaries(data);

       //BENEFICIARIES
       {
           var replace = "The project(s) will support approximately ";
           if (data["INDICATOR_SELECTION/O1"] != null) {
               replace = replace + tbene["tbene1"].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + " individuals for agriculture production";
           }

           if (data["INDICATOR_SELECTION/O1"] != null && data["INDICATOR_SELECTION/O2"] != null) {
               replace = replace + ", " + tbene["tbene2"].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + " individuals for self-employment";
           }

           if (data["INDICATOR_SELECTION/O3"] != null) {
               replace = replace + ", " + tbene["tbene3"].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + " individuals for wage-employment";
           }
           replace = replace + " among " + POC[1].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

           var totalhost = tbene["tbene4"] + tbene["tbene5"] + tbene["tbene6"];
           if (totalhost > 0) {
               replace = replace + ", as well as " + totalhost.toString().replace(/\B(?=(\d{3})+(?!\d))/g) + " individuals among the host population";
           }

           replace = replace + ".";

           text = text.replace("<?AREA2?>", replace);
       }

       //INTERVENTIONS
       {
           var replace = "";
           var count = 0;

           if (data["INDICATOR_SELECTION/OUTPUT1/HA1"] != null || data["INDICATOR_SELECTION/OUTPUT2/HA2"] != null || data["INDICATOR_SELECTION/OUTPUT3/HA3"] != null) {
               replace = replace + ", ?#? skills training";
               count++;
           }

           if (data["INDICATOR_SELECTION/OUTPUT1/SA1"] != null || data["INDICATOR_SELECTION/OUTPUT2/SA2"] != null || data["INDICATOR_SELECTION/OUTPUT3/SA3"] != null) {
               replace = replace + ", ?#? organisational support";
               count++;
           }

           if (data["INDICATOR_SELECTION/OUTPUT1/FPA1"] != null || data["INDICATOR_SELECTION/OUTPUT2/FPA2"] != null || data["INDICATOR_SELECTION/OUTPUT3/FPA3"] != null) {
               replace = replace + ", ?#? productive and/or financial assets";
               count++;
           }

           if (data["INDICATOR_SELECTION/OUTPUT1/FA1"] != null || data["INDICATOR_SELECTION/OUTPUT2/FA2"] != null || data["INDICATOR_SELECTION/OUTPUT3/FA3"] != null) {
               replace = replace + ", ?#? support to access financial services";
               count++;
           }

           if (data["INDICATOR_SELECTION/OUTPUT1/PD1"] != null) {
               replace = replace + ", ?#? support for product differenciation";
               count++;
           }

           if (data["INDICATOR_SELECTION/OUTPUT1/MA1"] != null || data["INDICATOR_SELECTION/OUTPUT2/MA2"] != null) {
               replace = replace + ", ?#? support for better market access";
               count++;
           }

           if (count > 1) {
               replace = replace.replace(",", "");
               replace = replace.replace(/,([^,]*)$/, ' and ' + "$1");
               replace = replace.replace("?#?", "1)");
               replace = replace.replace("?#?", "2)");
               replace = replace.replace("?#?", "3)");
               replace = replace.replace("?#?", "4)");
           } else { replace = replace.replace(", ?#?", ""); }

           if (data["PROBLEM_STATEMENT/STRATEGY/v_graduation"] == "Yes") {
               replace = replace + " as a part of graduation programme"
           }

           replace = replace + ".";
           text = text.replace("<?AREA1?>", replace);
       }

       //#endregion

       Browser.msgBox(text);

}