Reputation: 789
I have this code in a google spreadsheet:
//Creo degli oggetti contenenti i fogli
var ss = SpreadsheetApp.getActiveSpreadsheet();
var eu = ss.getSheetByName("EntrateUscite");
var ultimaRigaPiena = eu.getLastRow();
var primaRigaVuota = eu.getLastRow()+1;
var colTimeS =1;
var colTipo =2;
var colLav =3;
var colLavD =4;
var colInc =5;
var colIncD=6;
var colMese =7;
var colTargetMese =8;
var colGiorno=9;
var colTargetGiorno=10;
function doGet() {
return ContentService.createTextOutput('Hello, world!');
}
function entrata(){
entrataUscita("1");
}
function uscita(){
entrataUscita("0");
}
function entrataUscita(tipo) {
//Controllo se l'ultimo record è di tipo (un'entrata o un'uscita) diverso da quello corrente
if(eu.getRange(ultimaRigaPiena,colTipo).getValue()!=tipo){
//in se non lo è procedo
writeOnLastEmptyRow(colTimeS,getCurrTimeStamp());
writeOnLastRow(colTipo,tipo);
//Se il tipo è uscita allora calcolo quanto tempo è passato tra l'entrata e l'uscita con l'incrementale
if(tipo==0){
//Inserisco le ore lavorative come durata
eu.getRange(primaRigaVuota,colLav).setFormula("="+int2Let(colTimeS)+primaRigaVuota+"-"+int2Let(colTimeS)+(primaRigaVuota-1));
//Inserisco le ore lavorative come decimale
eu.getRange(primaRigaVuota,colLavD).setFormula("=TO_TEXT("+int2Let(colLav)+primaRigaVuota+")*24");
//Scrivo il mese
eu.getRange(primaRigaVuota,colMese).setFormula("=CONCATENATE(LOOKUP(MONTH("+int2Let(colTimeS)+primaRigaVuota+
");'Nomi mesi'!A1:B12);\" \";YEAR("+int2Let(colTimeS)+primaRigaVuota+"))");
//Scrivo il target mensile
eu.getRange(primaRigaVuota,colTargetMese).setFormula("=Target!E2")
//Scrivo il giorno
eu.getRange(primaRigaVuota,colGiorno).setFormula("=CONCATENATE(DAY("+int2Let(colTimeS)+primaRigaVuota+");\"/\";MONTH("+
int2Let(colTimeS)+primaRigaVuota+");\"/\";"+"YEAR("+int2Let(colTimeS)+primaRigaVuota+"))")
//Scrivo il terget giornaliero
eu.getRange(primaRigaVuota,colTargetGiorno).setFormula("=Target!C2")
//Se la cella con la quale devo fare l'addizione non è una durata
if(isValidDate(eu.getRange(primaRigaVuota-2,colInc).getValue())){
//la uso
eu.getRange(primaRigaVuota,colInc).setFormula("="+int2Let(colLav)+primaRigaVuota+"+"+int2Let(colInc)+(primaRigaVuota-2));
}else{
//altrimenti no
eu.getRange(primaRigaVuota,colInc).setFormula("="+int2Let(colLav)+primaRigaVuota);
}
//inserisco formula ore incrementeli in decimale
eu.getRange(primaRigaVuota,colIncD).setFormula("=TO_TEXT(" + int2Let(colInc)+primaRigaVuota +")*24");
}
}else{
if(tipo==1){
Browser.msgBox("Sei già dentro!");
}else{
Browser.msgBox("Sei già uscito!");
}
}
}
//Scrivo sul'ultima riga specificando la colonna ed il testo
function writeOnLastEmptyRow(column, text) {
eu.getRange(eu.getLastRow()+1,column).setValue(text);
}
//Scrivo sul'ultima riga specificando la colonna ed il testo
function writeOnLastRow(column, text) {
eu.getRange(eu.getLastRow(),column).setValue(text);
}
function getCurrTimeStamp(){
var oraCorrente = Utilities.formatDate(new Date(), "GMT+1", "dd-MM-yyyy HH.mm.ss");
return oraCorrente;
}
function isValidDate(value) {
var dateWrapper = new Date(value);
return !isNaN(dateWrapper.getDate());
}
function int2Let(n){
return String.fromCharCode(65 + n-1); // where n is 0, 1, 2 ... IL -1 SERVE PERCHE L'INDICIZZAZIONE PARTIREBBE DA 0
}
function prova(){
var prova = "sgh"
Browser.msgBox(int2Let(1))
}
I need to call the methods entrata and uscita from a site but when i try to shar the app the only message i get is: "TypeError: Impossible to call method "getSheetByName" of null."
try yourselves: Here
I dont understand why is not working and how to put 2 botton on an internet site to call those 2 methods.
TNX
Upvotes: 0
Views: 135
Reputation: 301
I'm not 100% sure what's going on with your code, both because I'm fairly new to doing this and because the comments are in Italian. However, I am assuming that you have created this script by creating a site, going to Manage Site > Apps Scripts > Add New Script (or editing an existing script). If that is what you are doing then the following will get you started:
First, you have to link to spreadsheets from sites by using the id, not directly, since the script is not "container bound" (directly linked from within the spreadsheet, or "bound" to the "container" spreadsheet). Here is an example of code where I've gotten the ID of spreadsheets from within my Google Drive from a script that is linked to a site ("standalone").
var files = DocsList.find("The Name of your Master Sheet where EntrateUscite is a sub sheet");
for (var i in files){
var fileId = files[i].getId();
}
var ss = SpreadsheetApp.openById(fileId);
var eu = ss.getSheetByName("EntrateUscite");
You may find more than one file with the same name as your sheet if it isn't unique enough. You may use:
var fileFolder = DocsList.getFolder('Folder Name');
//and then search within the folder like so:
var files = folderFromDocsListgetFolder.find("Spreadsheet Title");
//and then the for (var i in files) from above would go below this line
Just remember, you have to use the SpreadsheetApp.openById() method from Google Sites.
Also, I don't see any buttons or UI elements in your code, so those will be separate exercises. If I were you, I'd try to simplify what you're trying to do for now, and then make things more complex after you're more comfortable with the google apps interactions through scripts.
Upvotes: 1