How to test value of objects on a doGet function w

2019-07-24 07:46发布

Due to my bad english, and my low-level. I still find nothing about it. Maybe you could help me ?

I used to use Logger.log() to test content of things I want. I'm not an experienced developer, so I don't easily get in hand way to debug my code, especially on Google Apps Script, so maybe there is tricks to bypass my issue.

the problem is simple:

function doGet(e) {
  var currentUser = Session.getActiveUser().getEmail();
  var ssUrl =  e.parameter.url;
  var sheetName = e.parameter.sheet;
  var a1Notation = e.parameter.range;

As you can see, i use parameter passed by a Spreadsheet modification(well you can't see that it is a spreadsheet but you shouldn't care). But If run the script, It obviously see it as undefined and i can't try value of an object 10 lines after. I just would like to know if there is a way, such as a breakpoint, in order not to have to copy the URL String , sheet etc.. each time I need to debug.

1条回答
放荡不羁爱自由
2楼-- · 2019-07-24 08:22

The Script Editor offers a debugger and breakpoints, in case you haven't noticed.

In order to debug the doGet function, it's convenient to create another function that imitates a GET call. To begin with, I would create a logging web app:

function doGet(e) {
  return ContentService.createTextOutput(JSON.stringify(e));
}

Then get your source of GET requests call the logging web app instead of the real one. Record the JSON string returned and use it in a function such as this one.

function fakeGet() {
  var eventObject = 
    {
      "parameter": {
        "action": "view",
        "page": "3"
      },
      "contextPath": "",
      "contentLength": -1,
      "queryString": "action=view&page=3",
      "parameters": {
        "action": ["view"],
        "page": ["3"]
      }
    }
  doGet(eventObject);
}

Here fakeGet imitates a GET request call with query string ?action=view&page=3. You can launch fakeGet from script editor and use the debugger to observe how doGet function works.

查看更多
登录 后发表回答