Is there any way to debug a Spreadsheet app-script

2019-03-02 04:39发布

问题:

This question already has an answer here:

  • How can I test a trigger function in GAS? 2 answers

Is there any way to debug a Spreadsheet google app script at runtime? Just running it through the script editor is not useful, because the function I need to debug takes the trigger event as an argument.

回答1:

The simple answer is no - you can't debug on a form submit. You can however, write Logger.log statements in your onSubmit code and then write the log contents to say another spreadsheet or sheet which you can take a look later on.



回答2:

Not sure what your question about, but try this.

function get_color(e) {

  //var data = e.parameter.nameLabel

  var data = red;

  if (data == red){
    //...
  } else {
    //...
  }
}

so you can use the debugger to test your scripts without considering events

I hope it helps you!



回答3:

Here is a function that can test form submission trigger functions, lifted from How can I test a trigger function in GAS?.

function test_onFormSubmit() {
  var dataRange = SpreadsheetApp.getActiveSheet().getDataRange()
  var data = dataRange.getValues();
  var headers = data[0];
  // Start at row 1, skipping headers in row 0
  for (var row=1; row < data.length; row++) {
    var e = {};
    e.values = data[row];
    e.range = dataRange.offset(row,0,1,data[0].length);
    e.namedValues = {};
    // Loop through headers to create namedValues object
    for (var col=0; col<headers.length; col++) {
      e.namedValues[headers[col]] = e.values[col];
    }
    // Pass the simulated event to onFormSubmit
    onFormSubmit(e);
  }
}