2016-04-27 31 views
2

我正在嘗試打印Google表格中每日更新的最近一行數據。我正在對它進行API調用並將其返回給Json,同時我可以獲取最近的值,但我只能看到print the whole column of data: 我對代碼表示歉意,有點巴掌和破折號,我是新手。僅顯示Google表格中來自API調用的最近一行數據

的Javascript

var sheetsuUrl = "https://sheetsu.com/apis/v1.0/3e242af0"; 

    $.ajax({ 
    url: sheetsuUrl, 
    dataType: 'json', 
    type: 'GET', 

    // place for handling successful response 
    success: function(data) { 
     console.log(data[data.length-1]['moved']) 
     addCharacters(data); 
    }, 

    // handling error response 
    error: function(data) { 
     console.log(data); 
    } 
    }); 


    addCharacters = function(characters) { 
    var list = $('#ponies'); 
    for(var i=0; i<characters.length; i+=1) { 
     char = characters[i]; 
    function lastNoZero(myRange) { 
lastRow = myRange.length; 
for (; myRange[lastRow - 1] == "" || myRange[lastRow - 1] == 0 && lastRow > 0 ; lastRow--) { 
    /*nothing to do*/ 
} 
return myRange[lastRow - 1]; 
} 

myRange = char.moved;  
     if (char.moved == 'entered') { 
      html = "<img  src='https://media.firebox.com/pic/p5294_column_grid_12.jpg'/>"; 

     } else { 
      html = "<img src='http://41.media.tumblr.com/30b1b0d0a42bca3759610242a1ff0348/tumblr_nnjxy1GQAA1tpo3v2o1_540.jpg'/>"; 
     }; 
     list.append(html); 
    } 
    } 

HTML

<script src="https://code.jquery.com/jquery-3.0.0-alpha1.js"></script> 
<div id="ponies"></div> 

CSS

html img{ 
width:100px; 
height:100px; 
} 
+0

@olivereater看到更新的答案,現在應該工作。檢查預覽頂部顯示這是最新帖子的時間 –

回答

1

你需要引用您的GET請求返回數組中的最後一項。

characters[characters.length - 1];將得到字符數組中的最後一個字符。然後,爲確保在循環的每次運行中都不添加html,您需要將list.append(html);移至循環外,以確保它僅將內容附加到頁面一次。

運行下面的代碼片段以查看操作。

var sheetsuUrl = "https://sheetsu.com/apis/v1.0/3e242af0"; 
 

 
$.ajax({ 
 
url: sheetsuUrl, 
 
dataType: 'json', 
 
type: 'GET', 
 

 
// place for handling successful response 
 
success: function(data) { 
 
    addCharacters(data); 
 
}, 
 

 
// handling error response 
 
error: function(data) { 
 
    console.log(data); 
 
} 
 
}); 
 

 
addCharacters = function(characters) { 
 
    var list = $('#ponies'); 
 
    for(var i=0; i<characters.length; i+=1) { 
 
     char = characters[characters.length - 1]; 
 
    
 
    myRange = char.moved;  
 
     if (char.moved == 'entered') { 
 
      html = "<img  src='https://media.firebox.com/pic/p5294_column_grid_12.jpg'/>"; 
 

 
     } else { 
 
     html = "<img src='http://41.media.tumblr.com/30b1b0d0a42bca3759610242a1ff0348/tumblr_nnjxy1GQAA1tpo3v2o1_540.jpg'/>"; 
 
    }; 
 
    
 
    } 
 
    
 
    //for illustration purposes 
 
    list.append(characters[characters.length - 1].time) 
 
    
 
    //the code to attach the image 
 
    list.append(html); 
 
} 
 
<script src="https://code.jquery.com/jquery-3.0.0-alpha1.js"></script> 
 
<div id="ponies"></div>

+0

謝謝保羅,儘管現在這似乎只抓住了第一行的數據,即最早的數據,而不是最新的數據。 – Oliverater

+0

檢查更新後的答案 –

+0

謝謝Paul,這絕對是你創造我的一天的輝煌! – Oliverater

相關問題