有什么办法可以将Google表格单元格值作为动态变量添加到JSON Feed回调函数中?

时间:2019-07-13 23:33:14

标签: javascript jquery json google-sheets callback

有以下代码

<script type='text/javascript'>
        //<![CDATA[ 

          function favouritePosts(json) {
          j = (showRandomImg1) ? Math.floor((imgr.length + 1) * Math.random()) : 0;
          img = new Array();
          for (var i = 0; i < numposts; i++) {
            var entry = json.feed.entry[i];
            var posttitle = entry.title.$t;
            var pcm;
            var posturl;

            if (i == json.feed.entry.length) break;
            for (var k = 0; k < entry.link.length; k++) {
              if (entry.link[k].rel == 'alternate') {
                posturl = entry.link[k].href;
                break
              }
            }
            for (var k = 0; k < entry.link.length; k++) {
              if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
                pcm = entry.link[k].title.split(" ")[0];
                break
              }
            }
            if ("content" in entry) {
              var postcontent = entry.content.$t
              } else if ("summary" in entry) {
                var postcontent = entry.summary.$t
                } else var postcontent = "";
            postdate = entry.published.$t;
            if (j > imgr.length - 1) j = 0;
            img[i] = imgr[j];
            s = postcontent;
            a = s.indexOf("<img");
            b = s.indexOf("src=\"", a);
            c = s.indexOf("\"", b + 5);
            d = s.substr(b + 5, c - b - 5);
            if ((a != -1) && (b != -1) && (c != -1) && (d != "")) img[i] = d;

            var post_id = i;
            var postThumbSize = 600;
            var postThumb = entry.media$thumbnail.url;
            postThumb = postThumb.replace( '/s72-c/', '/s' + postThumbSize + '/' );
            var authorname = entry.author[0].name.$t; 
            var author_img = entry.author[0].gd$image.src;  
            var author_uri = entry.author[0].uri.$t;
            var label_category = entry.category[1].term;
            var address = $(postcontent).find("span.address").html(); 
            var map_point = $(postcontent).find("span.map_point").html(); 

        var item = '<div class="listing-item"><article class="geodir-category-listing fl-wrap"><div class="geodir-category-img"><img src="' + postThumb.replace( '/s72-c/', '/s' + 400 + '/' ) + '" alt=""><div class="overlay"></div></div><div class="geodir-category-content fl-wrap"><a class="listing-geodir-category" href="https://tinosemagazine-new.blogspot.com/search/label/'+label_category+'">'+label_category+'</a><div class="listing-avatar"><a href="'+author_uri+'"><img src="'+author_img+'" alt=""></a><span class="avatar-tooltip">Added By  <strong>'+authorname+'</strong></span></div><h3><a href="'+posturl+'">'+posttitle+'</a></h3><p>'+removeHtmlTag(postcontent,summaryPost)+'...</p><div class="geodir-category-options fl-wrap"><div class="listing-rating card-popup-rainingvis" data-starrating2=""><span>(17 reviews)</span></div><div class="geodir-category-location"><a href="#1" class="map-item"><i class="fa fa-map-marker" aria-hidden="true"></i>'+address+'</a><div id="1"></div></div></div></div></article></div>';
        document.write(item);
        }
}
 //]]>
 </script> 

通过json feed回调在html页面中给出结果...

<script src='/feeds/posts/summary/-/Νέα%20Καταχώρηση?alt=json-in-script&amp;callback=favouritePosts'/>

我想要的是,因为在json feed中没有关于评级的值,可以通过googlesheet api从google表格中获取这些值。 那是我在另一种情况下使用的一个函数,它从Google工作表读取和调用值。 但是是否可以在json feed回调函数中添加一个变量,该变量提供一个来自Google工作表的评分值(一个数字)? 正如上面的代码以某种方式在json favouritePosts函数中运行,以便每个帖子根据Google表格数据给出特定的评分值... 我知道您可以通过sq =(标题名称)= id_number来设置从Google工作表接收什么数据的条件... 因此,如果我将id_number设置为post_id变量(可能正在运行回调函数),以便从Google工作表接收有关post_id号码的不同数据。...

以下是对此的尝试,但结果令人疯狂。 我创建了一个作为值获取的函数,该值是我在收藏夹帖子函数

中设置的值

function getData(google){

       return $.ajax({
       url: 'https://spreadsheets.google.com/feeds/list/13zvQpUcOOwrFwJ6f9iiAKJSgrsdrYT_HqZNVR-BtwZI/od6/public/values?sq=urlb='+google+'&alt=json-in-script&callback=importGSS',
        dataType: 'jsonp',
        jsonpCallback: 'importGSS',
        success: function(json) {  


           var string = "";      
           var len = json.feed.entry.length;
            for (var i=0; i<len; i++) {   


            var rating = json.feed.entry[i].gsx$rating.$t;

              string += '<div class="listing-rating card-popup-rainingvis" data-starrating2="'+rating+'"></div>';      
            }

        $(string).appendTo('.listing-rating.card-popup-rainingvis');

                                }
                   })       
          }

和收藏夹

function favouritePosts(json) {
          j = (showRandomImg1) ? Math.floor((imgr.length + 1) * Math.random()) : 0;
          img = new Array();
          for (var i = 0; i < numposts; i++) {
            var entry = json.feed.entry[i];
            var posttitle = entry.title.$t;
            var pcm;
            var posturl;

            if (i == json.feed.entry.length) break;
            for (var k = 0; k < entry.link.length; k++) {
              if (entry.link[k].rel == 'alternate') {
                posturl = entry.link[k].href;
                break
              }
            }
            for (var k = 0; k < entry.link.length; k++) {
              if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
                pcm = entry.link[k].title.split(" ")[0];
                break
              }
            }
            if ("content" in entry) {
              var postcontent = entry.content.$t
              } else if ("summary" in entry) {
                var postcontent = entry.summary.$t
                } else var postcontent = "";
            postdate = entry.published.$t;
            if (j > imgr.length - 1) j = 0;
            img[i] = imgr[j];
            s = postcontent;
            a = s.indexOf("<img");
            b = s.indexOf("src=\"", a);
            c = s.indexOf("\"", b + 5);
            d = s.substr(b + 5, c - b - 5);
            if ((a != -1) && (b != -1) && (c != -1) && (d != "")) img[i] = d;

            var post_id = i;
            var postThumbSize = 600;
            var postThumb = entry.media$thumbnail.url;
            postThumb = postThumb.replace( '/s72-c/', '/s' + postThumbSize + '/' );
            var authorname = entry.author[0].name.$t; 
            var author_img = entry.author[0].gd$image.src;  
            var author_uri = entry.author[0].uri.$t;
            var label_category = entry.category[1].term;
            var address = $(postcontent).find("span.address").html(); 
            var map_point = $(postcontent).find("span.map_point").html();


        var item = '<div class="listing-item"><article class="geodir-category-listing fl-wrap"><div class="geodir-category-img"><img src="' + postThumb.replace( '/s72-c/', '/s' + 400 + '/' ) + '" alt=""><div class="overlay"></div></div><div class="geodir-category-content fl-wrap"><a class="listing-geodir-category" href="https://tinosemagazine-new.blogspot.com/search/label/'+label_category+'">'+label_category+'</a><div class="listing-avatar"><a href="'+author_uri+'"><img src="'+author_img+'" alt=""></a><span class="avatar-tooltip">Added By  <strong>'+authorname+'</strong></span></div><h3><a href="'+posturl+'">'+posttitle+'</a></h3><p>'+removeHtmlTag(postcontent,summaryPost)+'...</p><div class="geodir-category-options fl-wrap"><div class="listing-rating card-popup-rainingvis '+post_id+'" data-starrating2="'+getData(post_id)+'"><span>(17 reviews)</span></div><div class="geodir-category-location"><a href="#1" class="map-item"><i class="fa fa-map-marker" aria-hidden="true"></i>'+address+'</a><div id="1"></div></div></div></div></article></div>';
        document.write(item);
        }

结果是在所有帖子中给出相同的评级值,并且每次刷新页面时都会改变!!

0 个答案:

没有答案