简体   繁体   中英

Sharing google apps script

I have this code:

//Creo degli oggetti contenenti i fogli
//var ss = SpreadsheetApp.getActiveSpreadsheet();
//var eu = ss.getSheetByName("EntrateUscite");

var files = DocsList.find("Cartellino");
for (var i in files){
        var fileId = files[i].getId();
     }
      var ss = SpreadsheetApp.openById(fileId);
      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(e) {
  var app = UiApp.createApplication();

  //////////////////////////////////////////////////////////
  var buttonEntrata = app.createButton('Entrata');
  app.add(buttonEntrata);

  var labelEntrata = app.createLabel('Entrata!')
                 .setId('statusLabelEntrata')
                 .setVisible(false);
  app.add(labelEntrata);

  var handlerEntrata = app.createServerHandler('myClickHandlerEntrata');
  buttonEntrata.addClickHandler(handlerEntrata);

  /////////////////////////////////////////////////////////////
  var buttonUscita = app.createButton('Uscita');
  app.add(buttonUscita);

  var labelUscita = app.createLabel('Uscita!')
                 .setId('statusLabelUscita')
                 .setVisible(false);
  app.add(labelUscita);

  var handlerUscita = app.createServerHandler('myClickHandlerUscita');
  buttonUscita.addClickHandler(handlerUscita);

  return app;
}

function myClickHandlerEntrata(e) {
  var app = UiApp.getActiveApplication();

  var labelEntrata = app.getElementById('statusLabelEntrata');
  labelEntrata.setVisible(true);

  entrata()

  app.close();
  return app;
}

function myClickHandlerUscita(e) {
  var app = UiApp.getActiveApplication();

  var labelUscita = app.getElementById('statusLabelUscita');
  labelUscita.setVisible(true);

  uscita()

  app.close();
  return app;
}




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(YEAR("+
                                                       int2Let(colTimeS)+primaRigaVuota+");\"/\";TEXT(MONTH("
      +int2Let(colTimeS)+primaRigaVuota+");\"00\");\"/\";TEXT(DAY("+
        int2Let(colTimeS)+primaRigaVuota+");\"00\"))")



      //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(){
                                                  ///si aggiunge un'ora per l'ora legale 
  var oraCorrente = Utilities.formatDate(new Date().addHours(1), "GMT+1", "dd-MM-yyyy HH.mm.ss");
  return oraCorrente;
}



Date.prototype.addHours= function(h){
    this.setHours(this.getHours()+h);
    return this;
}



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))
}

The result should be a web page with 2 buttons.

but when i share it using the links that google gives me i get this error:

TypeError: Impossibile chiamare il metodo "getSheetByName" di null. TypeError: Impossible to call method "getSheetByName" of null. (english) :P

when instead i click "try your app with the last version of your code"on the sharing page i get another link that works....

why?

You have to save a new version of your code (file menu > manage versions) and update the version number in the deployment window with the number of this last version.

The other link you mention is the test version that allows you to test your code independently of the deployed version and that only you (and eventually script co-editors) can use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM