如何调试Google Apps脚本(又名Logger.log登录到哪里?) [英] How to debug Google Apps Script (aka where does Logger.log log to?)

查看:93
本文介绍了如何调试Google Apps脚本(又名Logger.log登录到哪里?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google表格中,您可以添加一些脚本功能。我为 onEdit 事件添加了一些内容,但我无法判断它是否正常工作。据我所知,您无法从Google表格中调试实时事件,因此您必须从调试器执行此操作,因为将事件参数传递给我的 onEdit()脚本编辑器运行它,code>函数总是未定义的。



试图在 onEdit 函数被调用时使用 Logger.log 方法来记录一些数据,但这看起来也是如此就像它只在从脚本编辑器运行时才起作用。当我从脚本编辑器运行它时,我可以通过转到查看 - >日志...



我希望能够在事件实际执行时看到日志,但我无法弄清楚。



我如何调试这些东西?

解决方案

Logger。 log 会向您发送(最终)发生在您的脚本中的错误的电子邮件,或者如果您正在从脚本编辑器 ,您可以通过转到查看 - >日志(仍在脚本编辑器中)查看上次运行功能的日志。同样,这只会向您显示从您在脚本编辑器 中运行的最后一个函数记录的任何内容。



我试图使用的脚本与电子表格有关 - 我制作了一个电子表格todo-checklist类型的东西,按优先顺序排序项目。



我为该脚本安装的唯一触发器是onOpen和onEdit触发器。调试onEdit触发器是最难解决的问题,因为我一直在想,如果我在onEdit函数中设置断点,打开电子表格,编辑单元格,我的断点将被触发。情况并非如此。



为了模拟编辑单元格,我最终不得不在实际的电子表格中执行某些操作。我所做的只是确保我希望它处理为已编辑的单元格被选中,然后在脚本编辑器中,我将转到 Run - > onEdit 。然后我的断点会被打。



然而,我不得不停止使用传入onEdit函数的事件参数 - 您无法通过执行运行> onEdit 。我需要从电子表格中得到任何信息,比如选择了哪个单元格等,我必须手动弄清楚。



不管怎样,很长的答案,但我最终还是找到了答案。



编辑

如果您想查看我制作的待办清单,可以在此处查看



(是的,我知道任何人都可以编辑它 - 这是分享它的重点!)

我希望它可以让你也看到了脚本。既然你在那里看不到它,这里是:
$ b

  function onOpen(){
setCheckboxes();
};

函数setCheckboxes(){
var checklist = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(checklist);
var checklist_data_range = checklist.getDataRange();
var checklist_num_rows = checklist_data_range.getNumRows();
Logger.log(checklist num rows:+ checklist_num_rows);

var coredata = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(core_data);
var coredata_data_range = coredata.getDataRange(); (var i = 0; i< checklist_num_rows-1; i ++){
var split = checklist_data_range.getCell(i + 2,3).getValue()。split( ||);
var item_id = split [split.length - 1];
if(item_id!=){
item_id = parseInt(item_id); (+(i + 2)+,2)设置为+ coredata_data_range.getCell(item_id + 1,3).getValue());
Logger.log
checklist_data_range.getCell(i + 2,2).setValue(coredata_data_range.getCell(item_id + 1,3).getValue());




函数onEdit(){
Logger.log(TESTING TESTING ON EDIT);
var active_sheet = SpreadsheetApp.getActiveSheet();
if(active_sheet.getName()==checklist){
var active_range = SpreadsheetApp.getActiveSheet()。getActiveRange();
Logger.log(active_range:+ active_range);
Logger.log(active range col:+ active_range.getColumn()+active range row:+ active_range.getRow());
Logger.log(active_range.value:+ active_range.getCell(1,1).getValue());
Logger.log(active_range.colidx:+ active_range.getColumnIndex());
if(active_range.getCell(1,1).getValue()==?|| active_range.getCell(1,1).getValue()==?){
Logger。日志(made it!);
var next_cell = active_sheet.getRange(active_range.getRow(),active_range.getColumn()+ 1,1,1).getCell(1,1);
var val = next_cell.getValue();
Logger.log(val:+ val);
var splits = val.split(||);
var item_id = splits [splits.length-1];
Logger.log(item_id:+ item_id);

var core_data = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(core_data);
var sheet_data_range = core_data.getDataRange();
var num_rows = sheet_data_range.getNumRows();
var sheet_values = sheet_data_range.getValues();
Logger.log(num_rows:+ num_rows); $($ i

) ] =+ sheet_values [i] [8]);
if(sheet_values [i] [8] == item_id){
Logger.log(found it!tyring to set it ...);
sheet_data_range.getCell(i + 1,2 + 1).setValue(active_range.getCell(1,1).getValue());
}
}

}
}

setCheckboxes();
};


In Google Sheets, you can add some scripting functionality. I'm adding something for the onEdit event, but I can't tell if it's working. As far as I can tell, you can't debug a live event from Google Sheets, so you have to do it from the debugger, which is pointless since the event argument passed to my onEdit() function will always be undefined if I run it from the Script Editor.

So, I was trying to use the Logger.log method to log some data whenever the onEdit function gets called, but this too seems like it only works when run from the Script Editor. When I run it from the Script Editor, I can view the logs by going to View->Logs...

I was hoping I'd be able to see the logs from when the event actually gets executed, but I can't figure it out.

How do I debug this stuff?

解决方案

Logger.log will either send you an email (eventually) of errors that have happened in your scripts, or, if you are running things from the Script Editor, you can view the log from the last run function by going to View->Logs (still in script editor). Again, that will only show you anything that was logged from the last function you ran from inside Script Editor.

The script I was trying to get working had to do with spreadsheets - I made a spreadsheet todo-checklist type thing that sorted items by priorities and such.

The only triggers I installed for that script were the onOpen and onEdit triggers. Debugging the onEdit trigger was the hardest one to figure out, because I kept thinking that if I set a breakpoint in my onEdit function, opened the spreadsheet, edited a cell, that my breakpoint would be triggered. This is not the case.

To simulate having edited a cell, I did end up having to do something in the actual spreadsheet though. All I did was make sure the cell that I wanted it to treat as "edited" was selected, then in Script Editor, I would go to Run->onEdit. Then my breakpoint would be hit.

However, I did have to stop using the event argument that gets passed into the onEdit function - you can't simulate that by doing Run->onEdit. Any info I needed from the spreadsheet, like which cell was selected, etc, I had to figure out manually.

Anyways, long answer, but I figured it out eventually.


EDIT:

If you want to see the todo checklist I made, you can check it out here

(yes, I know anybody can edit it - that's the point of sharing it!)

I was hoping it'd let you see the script as well. Since you can't see it there, here it is:

function onOpen() {
  setCheckboxes();
};

function setCheckboxes() {
  var checklist = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("checklist");
  var checklist_data_range = checklist.getDataRange();
  var checklist_num_rows = checklist_data_range.getNumRows();
  Logger.log("checklist num rows: " + checklist_num_rows);

  var coredata = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("core_data");
  var coredata_data_range = coredata.getDataRange();

  for(var i = 0 ; i < checklist_num_rows-1; i++) {
    var split = checklist_data_range.getCell(i+2, 3).getValue().split(" || ");
    var item_id = split[split.length - 1];
    if(item_id != "") {
      item_id = parseInt(item_id);
      Logger.log("setting value at ("+(i+2)+",2) to " + coredata_data_range.getCell(item_id+1, 3).getValue());
      checklist_data_range.getCell(i+2,2).setValue(coredata_data_range.getCell(item_id+1, 3).getValue());
    }
  }
}

function onEdit() {
  Logger.log("TESTING TESTING ON EDIT");
  var active_sheet = SpreadsheetApp.getActiveSheet();
  if(active_sheet.getName() == "checklist") {
    var active_range = SpreadsheetApp.getActiveSheet().getActiveRange();
    Logger.log("active_range: " + active_range);
    Logger.log("active range col: " + active_range.getColumn() + "active range row: " + active_range.getRow());
    Logger.log("active_range.value: " + active_range.getCell(1, 1).getValue());
    Logger.log("active_range. colidx: " + active_range.getColumnIndex());
    if(active_range.getCell(1,1).getValue() == "?" || active_range.getCell(1,1).getValue() == "?") {
      Logger.log("made it!");
      var next_cell = active_sheet.getRange(active_range.getRow(), active_range.getColumn()+1, 1, 1).getCell(1,1);
      var val = next_cell.getValue();
      Logger.log("val: " + val);
      var splits = val.split(" || ");
      var item_id = splits[splits.length-1];
      Logger.log("item_id: " + item_id);

      var core_data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("core_data");
      var sheet_data_range = core_data.getDataRange();
      var num_rows = sheet_data_range.getNumRows();
      var sheet_values = sheet_data_range.getValues();
      Logger.log("num_rows: " + num_rows);

      for(var i = 0; i < num_rows; i++) {
        Logger.log("sheet_values[" + (i) + "][" + (8) + "] = " + sheet_values[i][8]);
        if(sheet_values[i][8] == item_id) {
          Logger.log("found it! tyring to set it...");
          sheet_data_range.getCell(i+1, 2+1).setValue(active_range.getCell(1,1).getValue());
        }
      }

    }
  }

  setCheckboxes();
};

这篇关于如何调试Google Apps脚本(又名Logger.log登录到哪里?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆