2014-02-25 45 views
0

我有這樣的代碼在谷歌電子表格:奇怪的錯誤處理谷歌電子表格

//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鈕上的互聯網網站調用這些2種方法。

TNX

+0

對不起,意大利語意見:P – AndreA

回答

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 

只要記住,你必須使用SpreadsheetApp.openById從谷歌網站()方法。

此外,我沒有在代碼中看到任何按鈕或UI元素,因此這些將是單獨的練習。如果我是你,我會嘗試簡化你現在要做的事情,然後在你更熟悉通過腳本進行的谷歌應用交互之後,讓事情變得更加複雜。

相關問題