Common Script

Function CopyLMtoEM

CopyLMtoEM(srcProjectId, dstProjectId)

The function copies the common script from Livelihoods monitoring to Energy Monitoring

Arguments
  • srcProjectId (string) – Source project ID = LM

  • dstProjectId (string) – Destination spreadsheet ID = EM

 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
function CopyLMtoEM() {
    var srcProjectId = "1HFk0_oLrWQ9bkDWMOV3kphLrwadGVycSPsATVRjIbobyBkZpmwFR82T1"; // Source project ID = LM
    var dstProjectId = "1Ka9_9P8LZ1YnpvhOZKhb0JlJd1yloue71c7_LtB4E-wmkEujR3gyyZ8m"; // Destination spreadsheet ID = EM

    var baseUrl = "https://script.googleapis.com/v1/projects";
    var accessToken = ScriptApp.getOAuthToken();

    // Retrieve bound-script project.
    var obj = UrlFetchApp.fetch(baseUrl + "/" + dstProjectId + "/content", {
        method: "get",
        headers: { "Authorization": "Bearer " + accessToken }
    }).getContentText();

    var obj = JSON.parse(obj);
    var functiontext;

    for (var i = 0; i < obj["files"].length; i++) {
        if (obj["files"][i]["name"] == "ENERGY SPECIFIC") {
            functiontext = obj["files"][i];
            break;
        }
    }

    var obj2 = UrlFetchApp.fetch(baseUrl + "/" + srcProjectId + "/content", {
        method: "get",
        headers: { "Authorization": "Bearer " + accessToken }
    }).getContentText();

    var obj2 = JSON.parse(obj2);
    for (var i = 0; i < obj2["files"].length; i++) {
        if (obj2["files"][i]["name"] == "LIVELIHOODS SPECIFIC") {
            delete obj2.files[i];
            break;
        }
    }

    obj2["files"].push(functiontext);
    var obj2 = JSON.stringify(obj2);

    var res = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + dstProjectId + "/content", {
        method: "put",
        contentType: 'application/json',
        headers: { "Authorization": "Bearer " + accessToken },
        payload: obj2
    }).getContentText());
}