【问题标题】:Add OnEdit to a script or stop running script after it calculates cell in Google Sheets? Service invoked too many times Try Utilities.sleep(1000)将 OnEdit 添加到脚本或在计算 Google 表格中的单元格后停止运行脚本?服务调用次数过多尝试 Utilities.sleep(1000)
【发布时间】:2020-03-29 19:15:15
【问题描述】:

如何将 OnEdit 添加到脚本或防止脚本在特定的 Google 表格单元格中多次运行?我正在使用 Google 地图计算 2 点之间的时间,但服务调用次数过多尝试 Utilities.sleep(1000)。

我正在使用的脚本:


/**
* Get Distance between 2 different addresses.
* @param start_address Address as string Ex. "300 N LaSalles St, Chicago, IL"
* @param end_address Address as string Ex. "900 N LaSalles St, Chicago, IL"
* @param return_type Return type as string Ex. "miles" or "kilometers" or "minutes" or "hours"
* @customfunction
*/

function GOOGLEMAPS(start_address,end_address,return_type) {

  // https://www.chicagocomputerclasses.com/
  // Nov 2017
  // improvements needed

  var mapObj = Maps.newDirectionFinder();
  mapObj.setOrigin(start_address);
  mapObj.setDestination(end_address);
  var directions = mapObj.getDirections();

  var getTheLeg = directions["routes"][0]["legs"][0];

  var meters = getTheLeg["distance"]["value"];

  switch(return_type){
    case "miles":
      return meters * 0.000621371;
      break;
    case "minutes":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to minutes and return
        return duration / 60;
      break;
    case "hours":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to hours and return
        return duration / 60 / 60;
      break;      
    case "kilometers":
      return meters / 1000;
      break;
    default:
      return "Error: Wrong Unit Type";
   }

}

我希望脚本在计算出值后停止在特定电子表格单元格上运行。 有什么想法吗? 谢谢!

【问题讨论】:

    标签: google-maps google-apps-script google-sheets triggers spreadsheet


    【解决方案1】:

    在脚本末尾使用SpreadsheetApp.getActive().getSheetByName("Name").getRange("Select Range").activate(); 怎么样?

    【讨论】:

    • 谢谢,我已添加到脚本中,如下所示。是您建议的正确方式还是我需要将工作表的实际名称和范围添加到您的附加代码中,例如:SpreadsheetApp.getActive().getSheetByName(Sheet1).getRange(B:B).activate();
    • 是的,您需要添加要结束的工作表名称和单元格范围。
    【解决方案2】:

    或者这是添加它的正确方法吗?我在最后两个括号之间添加了代码,并保持“名称”和“选择范围”文本原样:

    
    /**
    * Get Distance between 2 different addresses.
    * @param start_address Address as string Ex. "300 N LaSalles St, Chicago, IL"
    * @param end_address Address as string Ex. "900 N LaSalles St, Chicago, IL"
    * @param return_type Return type as string Ex. "miles" or "kilometers" or "minutes" or "hours"
    * @customfunction
    */
    
    function GOOGLEMAPS(start_address,end_address,return_type) {
    
      // https://www.chicagocomputerclasses.com/
      // Nov 2017
      // improvements needed
    
      var mapObj = Maps.newDirectionFinder();
      mapObj.setOrigin(start_address);
      mapObj.setDestination(end_address);
      var directions = mapObj.getDirections();
    
      var getTheLeg = directions["routes"][0]["legs"][0];
    
      var meters = getTheLeg["distance"]["value"];
    
      switch(return_type){
        case "miles":
          return meters * 0.000621371;
          break;
        case "minutes":
            // get duration in seconds
            var duration = getTheLeg["duration"]["value"];
            //convert to minutes and return
            return duration / 60;
          break;
        case "hours":
            // get duration in seconds
            var duration = getTheLeg["duration"]["value"];
            //convert to hours and return
            return duration / 60 / 60;
          break;      
        case "kilometers":
          return meters / 1000;
          break;
        default:
          return "Error: Wrong Unit Type";
       }
      SpreadsheetApp.getActive().getSheetByName("Name").getRange("Select Range").activate();
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多