mtnrocky
mtnrocky

Reputation: 17

google sheets function won't run after another function

I have two google apps scripts functions ( plootorawtofinalforqbse() and myFunction() ) that work fine independently but I'd like to combine the two functions into one google apps script. If I do combine them, myfunction() does not move the columns

I can run them separately as different scripts and myfunction() works.

function plootorawtofinalforqbse() {
var sheet = SpreadsheetApp.getActive();
var lastCol = sheet.getLastColumn();
var keep = [1,4,21]; // array of column numbers to keep
sheet.deleteRow(1);

for (var col=lastCol; col > 0; col--) {
if (keep.indexOf(col) == -1) {
// This isn't a keeper, delete it
sheet.deleteColumn(col);
SpreadsheetApp.flush();
 }
}
  
  };
function myFunction() {
const sheet = 
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Transaction Details");
sheet.moveColumns(sheet.getRange("A1"), 3);
}

How to I put them together into one google apps script?

sheet link for testing script

Upvotes: 0

Views: 862

Answers (1)

Alessandro
Alessandro

Reputation: 2998

Function Declaration

You are declaring two functions in your Apps Script Project:

function lootorawtofinalforqbse() {
//...
}

function myFunction() {
//...
}

These two functions are now declared. A function declaration tells the Apps Script engine about a function's name, return type, and parameters.

Function Execution

To run a declared function you can use the Apps Script IDE feature: From the top menu Run>Run Function>{Function Name} where {Function Name} could be one of the declared function names. Since your goal is to execute these two function in order, I would recommend to declare a third function: main().

In programming the main() function is a standard entry-point for programs. Its main goal is to execute multiple functions that will achieve the program functionality.

So your main() function would look like this:

function main() {  

    lootorawtofinalforqbse();  

    myFunction(); 
}

Solution

Running the main() function from the Apps Script IDE you will achieve the ordered execution of your two functions.

Other Examples

Here you will find other examples on how to program using Google Apps Script

Upvotes: 1

Related Questions