1

我使用Google腳本來填充Google Analytics數據的Google Spreadsheet。
問題:我從我的電子表格文件中調用=getCountOfNewUsers(B1,B2)時得到error: Login Required
注意:當我從谷歌腳本項目運行runTest()一切工作正常。 供參考:我跟着this教程。從Google電子表格調用Google AnalyticsAPI時出現'需要登錄'錯誤

function runTest() { 
    try { 
    var results = getCountOfNewUsers(new Date(), new Date()); 
    Logger.log(results.totalsForAllResults["ga:newVisits"]); 

    } catch(error) { 
    Logger.log(error.message); 
    } 
} 

function getCountOfNewUsers(date_from, date_till) { 
    date_from = Utilities.formatDate(date_from, 'GMT', 'yyyy-MM-dd'); 
    date_till = Utilities.formatDate(date_till, 'GMT', 'yyyy-MM-dd');  
    var tableId = 'ga:*****'; 

    // Make a request to the API. 
    var results = Analytics.Data.Ga.get(tableId, date_from, date_till, 'ga:newVisits', {}); 

    if (results.getRows()) { 
    return results;  
    } else { 
    throw new Error('No views (profiles) found'); 
    } 
} 
+0

嗨Oleg - 我現在遇到了這個問題從單元格)。你有沒有找到解決方法? – bloo

+0

原來,你不能從一個單元格函數中完成它:https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services – bloo

回答

1

這不是解決你的問題,但如果你的興趣是引導您輕鬆更好的解決您的問題!你可以輕鬆地設置該解決方案在一個小時!

詳情:

效果優於勤奮學習書寫我們自己的腳本,使谷歌的腳本來填補谷歌電子表格由谷歌Analytics的數據我用這個Google Analytics Report Automation (Magic Script)

  • 這個神奇的腳本允許您可以簡化此過程並使其容易獲取所需的數據,以便您可以專注於分析和 報告。
  • 定製API儀表板 - 無需代碼它可以節省您編寫 編寫您自己的腳本的負擔。

enter image description here

關於錯誤,請嘗試以下的事情

  • 轉到腳本編輯器,單擊資源>點擊使用谷歌的API。
  • 點擊「Google API控制檯」。
  • 選擇API &驗證>點擊註冊的應用程序。
  • 請將您的腳本項目註冊爲Web應用程序。
  • 選擇證書>生成新密鑰。
+1

謝謝,但我找不到APIs& auth標籤http://imgur.com/x40YwzN –

0

這是一個預期的行爲。自定義功能不能要求用戶訪問個人數據。所以它們不適合所有類型的服務,特別是那些需要授權個人數據的服務。

檢查URL以便知道你可以使用哪些服務 - https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services

與大多數其他類型的應用程序的腳本,自定義的功能不要求用戶授權訪問個人數據。因此,他們只能撥打無法訪問個人數據的服務

相關問題