Json 例外:范围中的行数必须至少为1

Json 例外:范围中的行数必须至少为1,json,google-apps-script,google-sheets,Json,Google Apps Script,Google Sheets,我在将json数据写入google工作表时遇到问题-我得到一个异常“数据中的行数与范围中的行数不匹配” 我认为问题在于如何设置范围,但我不知道如何解决它。我在这个脚本后面包含了一个json数据示例: function getApi() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("Sheet1"); var url = "https:/

我在将json数据写入google工作表时遇到问题-我得到一个异常“数据中的行数与范围中的行数不匹配”

我认为问题在于如何设置范围,但我不知道如何解决它。我在这个脚本后面包含了一个json数据示例:

  function getApi() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var url = "https://www.lexology.com/api/v1/track/clients/articles?companyId=1013419&limit=100";
  var params = {
         "contentType": "application/json",
         "headers":{"ApiKey": "MYAPIKEY"
                    },

         };

var response = UrlFetchApp.fetch(url, params);
var dataAll = JSON.parse(response.getContentText()); 
var dataSet = dataAll;
var rows = [],
data;
  

  for (i = 0; i < dataSet.length; i++) {
    data = dataSet[i];
    rows.push([data.companyID, data.companyName, data.articles ]); 
  }
 Logger.log(dataSet)

  dataRange = sheet.getRange(3, 1, rows.length, 3); 
  dataRange.setValues(rows);
}
编辑

我已经修复了代码中指出的拼写错误,谢谢

我正在寻找的输出在中列出

另外,当我执行Logger.log(dataAll)时,它不是空的。我得到以下信息:

12:58:42 AM Info    Logging output too large. Truncating output. {workAreas=[], articles=[{jurisdictions=[Mexico, USA], forwardedUsingMailTo=0.0, topic=null, prints=0.0, date=2021-01-04T00:00:00, authorNameOrPhotoClick=0.0, viewOriginal=0.0, workAreas=[Patents], title=USPTO and IMPI Announce Parallel Patent Grant Initiative, firmName=Casimir Jones SC, summary=Under the terms of the MoU, a parallel patent grant framework was instituted which allowed IMPI to leverage search and examination results from the USPTO when issuing a counterpart Mexican patent., reads=3.0, articleContentLink=0.0, articleUrl=https://www.lexology.com/library/detail.aspx?g=253372ab-8cce-47c9-853f-4a9953becc0d}, {articleUrl=https://www.lexology.com/library/detail.aspx?g=2c23065f-8479-4343-a889-2a072465bedc, authorNameOrPhotoClick=0.0, prints=0.0, jurisdictions=[USA], viewOriginal=0.0, topic=null, date=2020-12-23T00:00:00, reads=3.0, firmName=Finnegan, Henderson, Farabow, Garrett & Dunner, LLP, forwardedUsingMailTo=0.0, articleContentLink=0.0, summary=Finding the correct balance requires the assistance of a patent strategist with knowledge of what the patent examiner is likely to accept, and who is skilled in crafting the patent in a way that covers what competitors will need in order to compete., title=Why Everyone Is Patenting Software Inventions, workAreas=[IT & Data Protection, Patents]}, {date=2021-01-12T00:00:00, prints=0.0, reads=3.0, title=Election Ballot Verification - A Patent Subject Matter Eligibility Analysis, summary=v. CLS Bank Int., the court (1) determined that the claims were drawn to an abstract idea of “voting, verifying the vote, and submitting the vote for tabulation” and (2) found “no inventive concept in the claims sufficient to transform them into patent-eligible subject matter.” Recited at a high level of abstraction, the claims at issue before the court included one or more voting stations, each including a computer, a display, a printer, an input device, a ballot scanning machine, and a means for tabulating printed ballots., workAreas=[Litigation, Patents], jurisdictions=[USA], authorNameOrPhotoClick=0.0, forwardedUsingMailTo=0.0, topic=null, viewOriginal=0.0, articleContentLink=0.0, articleUrl=https://www.lexology.com/library/detail.aspx?g=544fc695-b8eb-4951-a19e-c2df4b7039b6, firmName=Baker & Hostetler LLP}, {authorNameOrPhotoClick=0.0, topic=null, forwardedUsingMailTo=0.0, date=2021-02-04T00:00:00, reads=3.0, articleUrl=https://www.lexology.com/library/detail.aspx?g=64b1aa84-d37f-4e80-9805-3509b48c1824, workAreas=[IT & Data Protection, Litigation], jurisdictions=[USA], title=Successful Dismissal of PayPal Class Action Over Breach Disclosures Serves as Risks Reminder, summary=Companies should consult closely with counsel when making a public announcement regarding a potential or confirmed data security incident to ensure they are thinking through the potential regulatory and litigation risks, whether a trading blackout period is appropriate during the period of investigation, and whether existing cybersecurity risk disclosures in the company’s public filings should be amended., firmName=Sheppard Mullin Richter & Hampton LLP, articleContentLink=0.0, prints=0.0, viewOriginal=0.0}, {topic=null, firmName=Wilmer Cutler Pickering Hale and Dorr LLP, articleUrl=https://www.lexology.com/library/detail.aspx?g=6cad7e90-ff4a-413f-9a4d-079a23c12a44, prints=0.0, reads=3.0, date=2020-12-22T00:00:00, title=Quick Takeaways: SolarWinds Cybersecurity Incident, viewOriginal=0.0, authorNameOrPhotoClick=0.0, articleContentLink=0.0, forwardedUsingMailTo=0.0, workAreas=[IT & Data Protection], summary=Certain versions of SolarWinds software updates that assist organizations in managing their computer networks were compromised by what some officials, such as the US Secretary of State and the US Attorney General, have indicated is a Russian intelligence agency., jurisdictions=[USA]}, {firmName=Reed Smith LLP, forwardedUsingMailTo=0.0, authorNameOrPhotoClick=0.0, summary=The Guidelines set out the law applicable to operations consisting in reading or writing data on users’ devices and clarify the CNIL’s approach, while the Recommendation aims to guide organisations in their compliance efforts by providing examples of practical methods that can be used, notably to collect consent., date=2021-01-22T00:00:00, title=Adtech regulatory round-up January 2021, articleUrl=https://www.lexology.com/library/detail.aspx?g=8973269a-306a-48d3-9b19-0e50a6647d96, articleContentLink=0.0, viewOriginal=1.0, jurisdictions=[Germany, France, United Kingdom, USA], topic=null, workAreas=[IT & Data Protection, Litigation], reads=3.0, prints=0.0}, {firmName=Sheppard Mullin Richter & Hampton LLP, reads=3.0, articleUrl=https://www.lexology.com/library/detail.aspx?g=8e3a8f46-982e-41ba-a4bc-9e8af57ac7db, forwardedUsingMailTo=0.0, summary=The second carve-out from the definition of protected information was for information known to the employee before he joined TGS, “as evidenced by Employee’s written records.” The Brown court took a particularly dim view of this exception, because it would mean that information which, by definition was not confidential before Brown began working at TSG, would become so unless Brown had written records proving he knew of it beforehand., authorNameOrPhotoClick=0.0, title=California Court Strikes Down Overbroad Confidentiality Agreement as a de facto Non-Compete, articleContentLink=0.0, prints=0.0, topic=null, viewOriginal=0.0, date=2021-01-05T00:00:00, workAreas=[Employment & Labor, Litigation], jurisdictions=[USA]},
问题: 显然,您无法获得
0
行的范围。在此方法中,每个参数都应该是大于
0
的数字

为什么是
行。长度
0

这个问题与以下事实有关:
JSON
数据集
不是数组,因此
for
循环终止,因为
dataSet.length
未定义的
。因此,
row.length
为零,因为没有添加任何值

更新的解决方案: 假设工作表中的第一行(标题)为:

companyId companyName articles.date articles.articleUrl articles.title articles.summary

function getApi() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("Sheet1");
  const url = "https://www.lexology.com/api/v1/track/clients/articles?companyId=1013419&limit=100";
  const params = {
         "contentType": "application/json",
         "headers":{"ApiKey": "MYAPIKEY"
                    },

         };
         
  const response = UrlFetchApp.fetch(url, params);
  const dataSet = JSON.parse(response.getContentText()); 

  const data = 
              dataSet.articles.map((r,i)=>
              i==0?[dataSet.companyId,dataSet.companyName,r.date,r.articleUrl,r.title,r.summary]:
                   ["","",r.date,r.articleUrl,r.title,r.summary]
              );
  sheet.getRange(sheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}

我想当我看到您的示例JSON数据(这个JSON数据是
dataAll
?)和您的脚本时,
rows
[]
。我认为这可能是你的问题的原因。因此,为了正确理解您的目标,您可以使用示例JSON数据提供您期望的示例输出情况吗?这样,我想考虑一下脚本中的修改点。在OP的脚本中,
[]
,这是一个一维数组,因为for循环由于
数据集而没有运行。使用示例JSON数据时,长度
未定义的
。因此,我认为包含这些信息以及二维数组用于
setValues
的规范信息可能对OP和其他用户有用。@Tanaike非常感谢您的评论!如果
dataSet
是一个数组,并且所有参数都定义正确,那么
应该是一个2D数组,因为她在
for
循环中推送了一个数组。对吗?我对解决方案的答案进行了改进,但我们需要OP的一些澄清。请检查我答案中的最终注释,这些注释在代码中也是错误的。我认为您的方向是正确的。如果OP的JSON数据包含在数组中,
是二维数组。在这种情况下,OP的脚本不会发生错误。但是
data.articles
可能不是预期的结果。当我第一次从OP的错误消息中看到OP的问题时,我认为
可能是
[]
。由此,我假设OP在OP的问题中使用了JSON数据。所以我从这个问题中的JSON数据中询问了OP预期的示例输出情况。谢谢。我认为你的答案不仅对OP有用,对其他用户也有用。因此,当添加修改后的脚本时,答案将更加有用。我想对此进行投票。@Mardi LouiseVanHeerden您的json的格式为
{}
,它不是数组。因此,当调用
dataSet.length
时,会返回
undefined
错误,并且
不会被数据填充。给定此json,您只能获得一行数据。如果你确认,我可以提供一个解决方案。请同时阅读其他评论
data.articles
不是单个值,而是一个json对象数组。您需要从
文章
中获得哪些信息??
function getApi() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("Sheet1");
  const url = "https://www.lexology.com/api/v1/track/clients/articles?companyId=1013419&limit=100";
  const params = {
         "contentType": "application/json",
         "headers":{"ApiKey": "MYAPIKEY"
                    },

         };
         
  const response = UrlFetchApp.fetch(url, params);
  const dataSet = JSON.parse(response.getContentText()); 

  const data = 
              dataSet.articles.map((r,i)=>
              i==0?[dataSet.companyId,dataSet.companyName,r.date,r.articleUrl,r.title,r.summary]:
                   ["","",r.date,r.articleUrl,r.title,r.summary]
              );
  sheet.getRange(sheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}