2017-04-14 27 views
0

我剛剛開始我的JS旅程。目前正試圖在Google表格腳本中更新MailChimp API調用。問題在於,腳本(最初在此處找到:https://medium.hackinrio.com/mailchimp-reports-in-google-sheets-88496a153ee21)僅在保存Google腳本編輯器時起作用。我想創建一個JavaScript GAS函數,它能夠每天保存腳本編輯器,從而更新MailChimp計數。或者其他一些解決方案,可以讓這個工作。需要創建一個保存谷歌應用程序腳本(或修復原來的功能)的功能

谷歌腳本功能:

function getSubscribersCount(api_key, list_id) { 
    var API_KEY = api_key; 
    var LIST_ID = list_id; 

    var dc = '(my mailchimp API Key goes here)'.split('-')[1]; 
    var api = 'https://'+ dc +'.api.mailchimp.com/2.0'; 
    var membersPath = '/lists/members.json'; 

    // MC api-specific parameters 
    var payload = { 
    "apikey": API_KEY, 
    "id": LIST_ID 
    }; 

    // GAS specific parameters: 
    var params = { 
    "method": "POST", 
    "muteHttpExceptions": true, 
    "payload": payload 
    }; 

    var apiCall = function(endpoint){ 
    var apiResponse = UrlFetchApp.fetch(api+endpoint, params); 
    var json = JSON.parse(apiResponse); 
    return json; 
    }; 
var members = apiCall(membersPath); 
    return members.total; 
} 

是否有一個功能,我可以補充一點,使我得以保存功能,或者模擬CMD +的鍵盤事件?

回答

0

與其將公式寫入電子表格來調用腳本,您應該編寫另一個直接修改電子表格的函數。

function updateSubscriberCount() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(), 
     sheet = ss.getSheetByName("Sheet1"), 
     api_key = sheet.getRange("B1").getValue(), 
     list_id = sheet.getRange("C1").getValue(); 
    sheet.getRange("A1").setValue(getSubscribersCount(api_key, list_id)); 
} 

然後你點擊「編輯/當前項目的觸發器」並設置一個定時器以定期運行該功能。 但是,您必須至少手動運行該功能一次才能授予腳本所需的權限。

相關問題