【问题标题】:JSON.parse doesn't work with url linksJSON.parse 不适用于 url 链接
【发布时间】:2016-11-01 14:15:44
【问题描述】:

我是一名初级 javascript/google-apps-script 开发人员,我想为我在 Google 表格中的一些工作表添加一些功能。我正在处理许多 URL,需要跟踪作者最后一次修改它们的时间。

我已经构建了一些我认为可以工作的脚本,但显然(经过阅读后)需要一些专业的接触。

这个想法是遍历一列 URL (2500~) 并将每个 URL 的修改日期(来自其元数据)输出到右侧的单元格中。这是我的代码:

    function iteration1() {
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        **//The list to iterate on.**
        var sheet = ss.getSheetByName("Fund List");
        **//The column of which the links are stored**
        var urls = sheet.getRange("D2:D150").getValues();

        for (var row = 0; row < urls.length; row++) {
            for (var col = 0; col < urls[row].length; col++)
                **//Varifying if there is a URL within the cell**
                if (urls[row][col] != '') {
                    **//Storing each URL in a new array**
                    var url = UrlFetchApp.fetch(urls[row][col].valueOf());
                    **//Parsing the meta-data of the URL into an array**
                    var tweets = JSON.parse(url);
                    **//Retrieve the link modification date from the meta-data array & outputs to the cell from the right respectivley.**
                    sheet.getRange(row+2, 13).setValue(Logger.log(tweets[4][2]).getLog());
            }
        }
    }

例如:链接http://documents.financialexpress.net/Literature/37773008.pdf

它的元数据是:

{Accept-Ranges=bytes, X-Robots-Tag=noindex, nofollow, noarchive,nosnippet, Cache-Control=max-age=604800, Server=Microsoft-IIS/7.0, ETag="01827159b1d11:0", Access-Control-Allow-Origin=*, Access-Control-Allow-Methods=GET,PUT,POST,DELETE,OPTIONS, Last-Modified=Wed, 18 May 2016 23:00:00 GMT, Content-Length=113029, Access-Control-Allow-Headers=Content-Type, Date=Thu, 01 Sep 2016 11:43:52 GMT, Content-Type=application/pdf}

我只需要此元数据数组中的 Last-Modified 字段 Date 并将其输出到右侧的单元格。

提前感谢帮助者!很棒的社区!

我添加了当前代码和调试器模式的屏幕截图,其中提供了我正在处理的链接的示例:

【问题讨论】:

    标签: javascript json url google-apps-script


    【解决方案1】:

    根据我从谷歌文档 (https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String)) 中看到的结果,您存储在变量 url 中的结果不是字符串。

    JSON.parse 接受一个字符串并将其转换为 javascript Object/Array/String/whatever

    您需要使用此处记录的 JSON.parse(url), JSON.parse(url.getContentText('utf-8')) 而不是:https://developers.google.com/apps-script/reference/url-fetch/http-response

    【讨论】:

    • 我试过了,运行后它显示了这个错误:SyntaxError: Unexpected token: % (line 12, file "iteration1")
    • @TomerLevi 我需要更多信息,能否提供 url.getContentText('utf-8') 的内容示例?
    • 您好 Maurizio,我添加了一个屏幕截图,其中包含失败和正在处理的内容数组。谢谢;)
    • @TomerLevi 您正在尝试将 json 解析为 pdf 文件。如果我是对的,您的代码正在执行以下步骤:
    • 嗨 Maurizio,我正在尝试从链接元数据中检索上次修改日期如果此描述更好地解释了我的目标...
    【解决方案2】:

    经过几天的研究,我已经成功地检索了工作表中每个 URL 的 Last-Modified 日期键的值。


    我的代码:

    function iteration1() { 
      var ss = SpreadsheetApp.getActiveSpreadsheet();
        //The Google sheet to access
        var sheet = ss.getSheetByName("Sheet Name");
        //The array of URLs to check
        var urls = sheet.getRange("D2:D150").getDisplayValues();
    
        for (var row = 0; row < urls.length; row++) {
          for (var col = 0; col < urls[row].length; col++) {
            if (urls[row][col].toString() != '') {
              //Converting each URL to string and retrieving its Properties into a new Array
              var url = UrlFetchApp.fetch(urls[row][col].toString());
              var tweets = url.getAllHeaders();
    
              //Forming an array of Properties by Keys & Values
              var userProperties = PropertiesService.getUserProperties();
              userProperties.setProperties(tweets);
              var tweetsKeys = Object.keys(tweets);
            }
          }
    
          //Retrieving the link modification date from the property meta-data & outputs it as a String to the cell from the right respectivley.
          sheet.getRange(row+2, 12).setValue(userProperties.getProperty(tweetsKeys[7]));
        }
    }
    

    非常感谢您的回复!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      相关资源
      最近更新 更多