处理谷歌电子表格的谷歌应用程序脚本中的奇怪错误

时间:2014-02-25 16:46:24

标签: google-apps-script google-sheets

我在谷歌电子表格中有这个代码:

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

我需要从一个站点调用entrata和uscita方法,但是当我尝试使用应用程序时,我得到的唯一消息是:“TypeError:无法调用方法”getSheetByName“为null。”

尝试自己:Here

我不明白为什么不工作以及如何在互联网网站上放置2个按钮来调用这两种方法。

TNX

1 个答案:

答案 0 :(得分:1)

我不是100%肯定你的代码发生了什么,因为我这么做是相当新的,因为评论都是意大利语。但是,我假设您通过创建网站创建了此脚本,转到管理网站>应用程序脚本>添加新脚本(或编辑现有脚本)。如果这就是您正在做的事情,那么以下内容将帮助您入门:

首先,您必须使用id链接到网站的电子表格,而不是直接,因为脚本不是“容器绑定”(直接从电子表格中链接,或“绑定”到“容器”电子表格)。以下是一个代码示例,其中我从链接到网站的脚本(“独立”)中获取了Google云端硬盘中的电子表格ID。

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");

如果文件不够独特,您可能会找到多个与工作表同名的文件。你可以使用:

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

请记住,您必须使用Google协作平台中的SpreadsheetApp.openById()方法。

此外,我没有在您的代码中看到任何按钮或UI元素,因此这些将是单独的练习。如果我是你,我会尝试简化您现在要做的事情,然后在您通过脚本更熟悉谷歌应用程序交互后,让事情变得更加复杂。

相关问题