仅显示 Google 表格中来自 api 调用的最新数据行

Display only most recent row of data in Google Sheet from api call

本文关键字:调用 最新 数据 api Google 显示 表格      更新时间:2023-09-26

我正在尝试打印Google表格中的最新行,该表格每天更新。我正在对它进行 API 调用并以 Json 格式返回它,虽然我可以获取最新的值,但我似乎只能打印整列数据:我很抱歉代码有点耳光和破折号,我是新手。

爪哇语

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

您需要引用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>