How do you trigger a script in app script with app inventor?

Hi dear community,

Does any body knows how could I trigger a script code in a spreadsheet mit app inventor.
I mean when I press a buttom how could I run the code.

the let me give more detail about it. we have an app that send information to an spreadsheet, then this information needs to be handle I mean update some rows and delete another rows. and we also need to save a register for this information. in order to do it I create a code that can copy the las row "the row send" and past it in another sheet.

let me show you the code

function Duplic(){

const ss = SpreadsheetApp.getActiveSpreadsheet();

const fial = ss.getSheetByName('BaseHistorico').getLastRow();

const source = ss.getSheetByName('BaseHistorico').getRange(fial, 1, 1, 9);

const FilaFu = ss.getSheetByName('BaseActual').getLastRow();

const destine = ss.getSheetByName('BaseActual').getRange(FilaFu+1,1);

source.copyTo(destine)

but actually a do know how activate the code with pressing a buttom, by chance somebody know how shall we do it?

thanks in davance

You use either doGet(e) or doPost(e) in your apps script to receive external requests in order to activate the code set within.

From your app you send the request using the web component with an identifying parameter for the code you want to run (just like FN = Update)

In your doGet(e) you test with an if statement, then run the code you want to run.

For example:

In your app you send a GET request containing the parameter &FN=Duplic

In your apps script

function doGet(e) {

if ( e.parameter.FN == "Duplic" ) {
Duplic();
}
}

and you can have your Duplic() function on the same code sheet, outside of the doGet().

@TIMAI2 thanks for the answer, I am going to do it and I go back to say how does it works

TIMA

I Run the code like this

function Duplic(){

const ss = SpreadsheetApp.getActiveSpreadsheet();

const fial = ss.getSheetByName('REGISTROS').getLastRow();

const source = ss.getSheetByName('REGISTROS').getRange(fial, 1, 1, 9);

const FilaFu = ss.getSheetByName('HISTORICO').getLastRow();

const destine = ss.getSheetByName('HISTORICO').getRange(FilaFu+1,1);

source.copyTo(destine)

function doGet(e) {

var ss = SpreadsheetApp.openById(e.parameter.SHEETID);
var sh = ss.getSheetByName(e.parameter.SHEETNAME);
var rg = sh.getDataRange().getValues();
var Funcion = e.parameter.FUNCION

if (Funcion == 'ENVIAR' ) {
var registro = e.parameter.REGISTRO.split(',');
sh.appendRow(registro);
return ContentService.createTextOutput("New record created");
}

if ( Funcion == "Duplic" ) {
Duplic();

}

and the blocks like this

image

Is there a way to trigger bothe codes with the same buttom ?

I try to do like this but is not working

now I realize duplic is wrong is like this 'duplic' I am going to try like this and I go back
sir can you help me please

@Spicy_Topics

this is the question,

Do you know how we can trigger two codes at the same time only pressing a buttom ?

Place both apps script functions under the same FN (FUNCION) call from the AI2 app

1 Like

I did with blocks and it works good, thanks any way

I assume this is making two calls to the script, instead of one ?

Not, only one, what I did is to send the information since the biggining for bouth sheets

here the code

image

Sure looks like two calls to me...:wink: