Arturo
September 28, 2022, 10:47pm
1
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
TIMAI2
September 28, 2022, 11:04pm
2
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().
Arturo
September 28, 2022, 11:07pm
3
@TIMAI2 thanks for the answer, I am going to do it and I go back to say how does it works
Arturo
September 29, 2022, 12:58am
4
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
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
Arturo
September 29, 2022, 6:32am
5
@Still_Learning
this is the question,
Do you know how we can trigger two codes at the same time only pressing a buttom ?
TIMAI2
September 29, 2022, 3:43pm
6
Place both apps script functions under the same FN (FUNCION) call from the AI2 app
1 Like
Arturo
September 29, 2022, 9:42pm
7
I did with blocks and it works good, thanks any way
TIMAI2
September 29, 2022, 9:54pm
8
I assume this is making two calls to the script, instead of one ?
Arturo
September 29, 2022, 9:57pm
9
Not, only one, what I did is to send the information since the biggining for bouth sheets
here the code
TIMAI2
September 29, 2022, 10:03pm
10
Sure looks like two calls to me...