【发布时间】:2018-04-10 16:51:20
【问题描述】:
我需要修复我一直在处理的 Google 脚本。基本上,我有一个 json https://www.instagram.com/nike/?__a=1,它返回有关耐克在 Instagram 上的帐户的基本信息。我从诸如“传记”之类的对象中检索数据没有问题。但是,当我尝试检索嵌套对象(数组)时,我做错了,因为结果重复出现(见附件)。谁能帮我弄清楚我做错了什么?
// the name of the sheet within your document
var sheetName = "Sheet1";
// the name of the Instagram account you want the follower count for
var instagramAccountName = "nike";
function insert() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(this.sheetName);
var followers = [];
var followers = captionArray(this.instagramAccountName);
for(var i = 0 ; i < 3; i++) {
sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), followers]);
};
}
function captionArray(username) {
var i = 0;
for(var i = 0; i < 3; i++) {
var url = "https://www.instagram.com/" + username + "/?__a=1";
var response = UrlFetchApp.fetch(url).getContentText();
var caption = [];
var caption = JSON.parse(response).graphql.user.edge_owner_to_timeline_media.edges[i].node.edge_media_to_caption.edges[i].node.text;
return caption;
};
}
【问题讨论】:
-
(没有附件)。您应该确保您的代码格式正确(阅读:缩进级别对于检查代码的组织很有用,并确定哪些语句在哪些循环中(也就是直观地定义“块范围”))。另请注意,
return语句的定位意味着您检索的内容永远不会超过第一个标题。
标签: json google-apps-script google-sheets instagram