如果 GoogleJsonResponseException: 则跳过并移至下一行

If GoogleJsonResponseException: then skip and move to next row

我有一个工作脚本。需要即兴发挥才能避免手动中断。我们在 Analytics 中有多个配置文件,有时我们失去访问权限,有时我们有。因此,当我 运行 脚本时,如果我们无法访问 60 个配置文件中的 1 个,我必须手动删除该条目,然后重新 运行 脚本。 我想要的是,如果出现以下错误,则跳过并继续下一行

“GoogleJsonResponseException:API 调用 analytics.data.ga.get 失败,出现错误:用户对此配置文件没有足够的权限。”

function GoogleAnalytics() {
 
  var doc2 = SpreadsheetApp.getActiveSpreadsheet();
  var dashboard = doc2.getSheetByName("Dashboard");
  for(var i=52;i<65;i++){ 
  var viewId = dashboard.getRange(i,13).getValue(); // Your Google Analytics view ID
 
  
        
  var metric = 'ga:metric, ga:metric2, ga:metric3';
  var option = {'segment': 'gaid::-5'};
  
       
   var result = Analytics.Data.Ga.get(viewId, metric, option);


    var metric = result.totalsForAllResults['ga:metric'];
    var metric2 = result.totalsForAllResults['ga:metric2'];
    var metric3 = result.totalsForAllResults['ga:metric3'];
  
        var doc = SpreadsheetApp.getActiveSpreadsheet(); // Current document
        var sheet = doc.getActiveSheet(); // Current sheet
  
    sheet.getRange(i,14,1,1).setValue(metric); 
    sheet.getRange(i,15,1,1).setValue(metric2); 
    sheet.getRange(i,16,1,1).setValue(metric3); 
} }

我可能对问题的理解不正确(如果是,请澄清)但在我看来你只需要添加...

function GoogleAnalytics() {
 
  var doc2 = SpreadsheetApp.getActiveSpreadsheet();
  var dashboard = doc2.getSheetByName("Dashboard");
  for(var i=52;i<65;i++){ 
  try { //...this line and...
  var viewId = dashboard.getRange(i,13).getValue(); // Your Google Analytics view ID
 
  
        
  var metric = 'ga:metric, ga:metric2, ga:metric3';
  var option = {'segment': 'gaid::-5'};
  
       
   var result = Analytics.Data.Ga.get(viewId, metric, option);


    var metric = result.totalsForAllResults['ga:metric'];
    var metric2 = result.totalsForAllResults['ga:metric2'];
    var metric3 = result.totalsForAllResults['ga:metric3'];
  
        var doc = SpreadsheetApp.getActiveSpreadsheet(); // Current document
        var sheet = doc.getActiveSheet(); // Current sheet
  
    sheet.getRange(i,14,1,1).setValue(metric); 
    sheet.getRange(i,15,1,1).setValue(metric2); 
    sheet.getRange(i,16,1,1).setValue(metric3); 
  } catch(e) { //...this part
    console.log(e); //optional, catch(e){} is perfectly valid as well, or any code you might want to execute on error
  }
} }

这样试试:

function GoogleAnalytics() {
  var doc2 = SpreadsheetApp.getActiveSpreadsheet();
  var sh = doc2.getSheetByName("Dashboard");
  var sheet = doc2.getActiveSheet(); // Current sheet
  const vs = sh.getRange(52, 13, 13).getValues();
  var metric = 'ga:metric, ga:metric2, ga:metric3';
  var option = { 'segment': 'gaid::-5' };
  for (var i = 0; i < vs.length; i++) {
    var viewId = vs[i][0]; // Your Google Analytics view ID
    try {
      var result = Analytics.Data.Ga.get(viewId, metric, option);
    }
    catch(e){
      continue;
    }
    if (result) {
      sheet.getRange(i + 52, 14, 1, 3).setValues([[result.totalsForAllResults['ga:metric'], result.totalsForAllResults['ga:metric2'], result.totalsForAllResults['ga:metric3']]]);
    }
  }
}

如果没有工作数据的好处,其中一些可能不正确,但使用 setValues 和 getValues 应该会大大加快它的速度,并且 try catch 块应该有助于避免始终如一地获得结果。您还想避免在循环中进行不必要的声明。