Andy Mac
Andy Mac

Reputation: 3

Clear values, not formulas

I have a script to copy values from one sheet to another that works perfectly except I only want to clear the values in the source sheet, and leave the formula in place in cells E3, I3 and J3

function addLeavers()
 {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getRange("Add Leaver!A3:AM3");
  var destSheet = ss.getSheetByName("Leavers");
  destSheet.appendRow(source.getValues()[0]);
  source.clear();
}

Upvotes: 0

Views: 300

Answers (1)

Tanaike
Tanaike

Reputation: 201358

  • You want to clear the range of source.
  • You want to clear only values of the range.
  • You don't want to clear the formulas of the range..

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

In this modification, the formulas are retrieve, and the range is clear using clear() or clearContent(). Then, the retrieved formulas are put to the range.

From:

source.clear();

To:

var formulas = source.getFormulas();
source.clear(): // or source.clearContent();
source.setFormulas(formulas);

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 4

Related Questions