Is there a other way to reset fields

Viewed 55

I'm trying to create a reset spreadsheet function to clear the spreadsheet. Now i use loops to define the values for each column. 'Dikte' Needs to be set to 6. And the first column needs to be false. clearContent clears everything. Is there a nicer way?

  var values = [];
  for (i = 0; i < 12; i++) {
    values.push([false,'','','','','6','0'])
  }
  var oldoak_data = SS.getRangeByName('oldoak_data').setValues(values);

Product table

1 Answers

Explanation:

Solution 1:

  • You could get rid of the for loop by using map. This will give you some boost in performance since map is faster than the regular loops.

  • Generally, since this approach overwrites the previous values, it is a good idea to clear the content before we set the new values.

Solution 2:

  • Another idea would be to define a sheet object by its name. In my case Sheet1. Clear only the desired range, 'A2:E13' and set the values of the other two columns to 6 and 0 respectively. Not sure if this approach is faster, but it could be more efficient.

Solutions:

Solution 1:

function resetFields() {
  const SS = SpreadsheetApp.getActive();
  const rng = SS.getRangeByName('oldoak_data');
  const values = rng.getValues().map(r=>[false,'','','','','6','0'])
  rng.clearContent();
  rng.setValues(values);
}

Solution 2:

function resetFields() {
  const SS = SpreadsheetApp.getActive();
  const SH = SS.getSheetByName('Sheet1');
  const rngClear = SH.getRange('A2:E13');
  rngClear.clearContent();
  SH.getRange('F2:F13').setValue(6);
  SH.getRange('G2:G13').setValue(0);
}

Example sheet for Solution 2:

example

Related