Manual control of SpreadsheetApp.flush()

I’ve written an Apps Scriptfunction, func() that moves data within multiple sheets, however Apps Script is bundling the operations in a way not optimal for user experience. Essentially what happens is that it is bundling all of thesetValue() operations up until the third sheet.getrange() call, in this case sheet3.getRalgeList.getRange(). I noticed this in testing by undoing the script operation and observing that more than one undo in google sheets is required to revert the changes. I want only one undo to be required to undo all changes brought on by this function. Calling SpreadsheetApp.flush() at the end does not resolve this issue. I’ve written other similar functions for my sheet and have never seen this issue before.

How can I fix this issue so that when the function is called, it can be undone with only one undo within Sheets? Right now, when the function is run, two undos are required to revert the changes. It’s evident that this has something to do with the way Apps Script is bundling operations. Is there a way to control this here?

function func() {
  let thing2Load = "Acc1"; 
  
  const sheet1          = func1().sheet;
  const sheet2      = func2().sheet;
  const sheet3         = func3().sheet;
  const var1       = _col_one();
  const var2    = _col_two();
  const var3        = _col_three();
  const var4      = _num_elements1();
  const var5     = _col_three();
  const var6   = _num_elements2();
  const var7              = start().startRow;
  const range1          = range1().range;
  const range2     = range2().range;
  const range3      = range3().range;
  const range4     = range4().range;
  const range5  = range5().range;
  const range6     = range6().range;
  const range7         = range7().range;
  const var9           = thingVals().length;
  const var10          = sheet1.getRange(range4).getValue();
  const var11               = sheet1.getRange(fields().inflows).getValues();
  const var12          = sheet1.getRange(range2).getValue();
  const var13          = sheet2.getRange(range3).getValues();
  const var14            = sheet3.getRange(range7).getValues();
  let var15                = sheet1.getRange(range1).getValues().filter(account => account[0] != "");
  let var16              = var15.map(row => row[0]).indexOf(thing2Load);
  let var17  = sheet2.getRange(var7, var5, sheet2.getMaxRows() - var7 + 1, var6 * var9).getValues();
  let var18    = sheet3.getRange(var7, var3, sheet3.getMaxRows() - var7 + 1, var4 * var9).getValues();
 
  const thingone2Load       = var15[var16][1];
  const thingtwo2Load     = var15[var16][2];
  const thingthree2Load   = var15[var16][3];
  const thingfour2Load    = var17.map(row => row.splice(var16 * var6, var6));
  const thingfive2Load         = var18.map(row => row.splice(var16 * var4, var4));

  var15.splice(var16, 1); 
  var15.push([var12, var10, var11[0], var11[2]]);
  for (let i = 0; i < var17.length; i++) { var17[i].push(...var13[i]); }
  for (let i = 0; i < var18.length; i++) { var18[i].push(...var14[i]); }

  const vals = [thing2Load, thingone2Load, thingtwo2Load, thingthree2Load, var15];
  const test = sheet1.getRange(var1, var2, var15.length, var15[0].length).getA1Notation();

  sheet1.getRangeList([range2, range4, range5, range6, test]).getRanges().forEach((range,idx) => {
      if (idx > 3) { range.setValues(vals[idx]) }
      if (idx <= 3) { range.setValue(vals[idx]); }
  });

  const vals1 = [thingfour2Load, var17];
  const test1 = sheet2.getRange(var7, var5, var17.length, var17[0].length).getA1Notation();

  sheet2.getRangeList([range3, test1]).getRanges().forEach((range,idx) => { range.setValues(vals1[idx]) });

  const vals2 = [thingfive2Load, var18];
  const test2 = sheet3.getRange(var7, var3, var18.length, var18[0].length).getA1Notation();

  sheet3.getRangeList([range7, test2]).getRanges().forEach((range,idx) => { range.setValues(vals2[idx]) });
  SpreadsheetApp.flush();

  return "<span style="font-weight: bold">" + '"'+  thing2Load + "" Thing Loaded</span>";
}