Home > Mobile >  I wrote a function that is triggered by on edit (e) but need to have this run inside a larger functi
I wrote a function that is triggered by on edit (e) but need to have this run inside a larger functi

Time:09-07

My script runs perfectly when triggered by editing the spreadsheet. I am needing to rewrite this so that it can be called and ran inside a larger function that has an installable trigger set to run weekly. My skills are limited and I am having trouble figuring this out. Below is the code I need to modify.

function archiveIVABPO(e) {
 var sheet = SpreadsheetApp.getActive().getSheetByName('IVA');
 const targetSheet = e.source.getSheetByName('Archived');
 const numColumnsToMove = 20;
 var Avals = sheet.getRange("A3:A").getValues();
 var Alast = Avals.filter(String).length; 
 const rangeToMove = sheet.getRange(e.range.rowStart, 1, Alast, numColumnsToMove);
 const values = rangeToMove.getValues();
 appendRowsV(targetSheet, values, 1);
 var vs = sheet.getRange('A3:T1500').setValue(null);
} 

I see the two lines of code that reference "e"

const targetSheet = e.source.getSheetByName('Archived');
const rangeToMove = sheet.getRange(e.range.rowStart, 1, Alast, numColumnsToMove);

What I am unsure of is how do I change this to be able to run without the onedit?

EDIT: my range to move needs to be all rows in column A starting in row 3enter image description here

CodePudding user response:

Based on the screenshot, The following replacements can be made:

  • e.source -> SpreadsheetApp.getActive()
  • e.range.rowStart -> 3

Use default parameters, so that it may run on both onEdit as well as calling without a parameter:

function archiveIVABPO(
  e = { source: SpreadsheetApp.getActive(), range: { rowStart: 3 } }
) {
//...Rest of your code
}
  • Related