Google 购物内容 Api - 仅列出需要的产品功能
Google Content Api for Shopping - List only wanted product features
我正在使用下面的脚本 (https://developers.google.com/apps-script/advanced/shopping-content) 我想知道,除了所有资源,我怎么能只列出产品价格?
/**
* Lists the products for a given merchant.
*/
function productList() {
var merchantId = 123456; // Replace this with your Merchant Center ID.
var pageToken;
var pageNum = 1;
var maxResults = 10;
do {
var products = ShoppingContent.Products.list(merchantId, {
pageToken: pageToken,
maxResults: maxResults
});
Logger.log('Page ' + pageNum);
if (products.resources) {
for (var i = 0; i < products.resources.length; i++) {
Logger.log('Item [' + i + '] ==> ' + products.resources[i]);
}
} else {
Logger.log('No more products in account ' + merchantId);
}
pageToken = products.nextPageToken;
pageNum++;
} while (pageToken);
}
你的情况,下面的修改怎么样?
发件人:
Logger.log('Item [' + i + '] ==> ' + products.resources[i]);
收件人:
Logger.log('Item [' + i + '] (product price) ==> ' + products.resources[i].price.value); // or products.resources[i].price.currency
注:
如果想以数组的形式获取数据,下面的修改如何?
function productList() {
var merchantId = 123456; // Replace this with your Merchant Center ID.
var pageToken;
var pageNum = 1;
var maxResults = 10;
var res = []; // Added
do {
var products = ShoppingContent.Products.list(merchantId, {
pageToken: pageToken,
maxResults: maxResults
});
Logger.log('Page ' + pageNum);
if (products.resources) {
for (var i = 0; i < products.resources.length; i++) {
res.push({id: products.resources[i].id, title: products.resources[i].title, price: products.resources[i].price.value}); // Added
Logger.log('Item [' + i + '] (product price) ==> ' + products.resources[i].price.value); // or products.resources[i].price.currency
}
} else {
Logger.log('No more products in account ' + merchantId);
}
pageToken = products.nextPageToken;
pageNum++;
} while (pageToken);
Logger.log(res); // Added
}
注:
- 在这个答案中,假设您已经能够使用
ShoppingContent.Products.list()
方法。请注意这一点。
参考文献:
我正在使用下面的脚本 (https://developers.google.com/apps-script/advanced/shopping-content) 我想知道,除了所有资源,我怎么能只列出产品价格?
/**
* Lists the products for a given merchant.
*/
function productList() {
var merchantId = 123456; // Replace this with your Merchant Center ID.
var pageToken;
var pageNum = 1;
var maxResults = 10;
do {
var products = ShoppingContent.Products.list(merchantId, {
pageToken: pageToken,
maxResults: maxResults
});
Logger.log('Page ' + pageNum);
if (products.resources) {
for (var i = 0; i < products.resources.length; i++) {
Logger.log('Item [' + i + '] ==> ' + products.resources[i]);
}
} else {
Logger.log('No more products in account ' + merchantId);
}
pageToken = products.nextPageToken;
pageNum++;
} while (pageToken);
}
你的情况,下面的修改怎么样?
发件人:
Logger.log('Item [' + i + '] ==> ' + products.resources[i]);
收件人:
Logger.log('Item [' + i + '] (product price) ==> ' + products.resources[i].price.value); // or products.resources[i].price.currency
注:
如果想以数组的形式获取数据,下面的修改如何?
function productList() { var merchantId = 123456; // Replace this with your Merchant Center ID. var pageToken; var pageNum = 1; var maxResults = 10; var res = []; // Added do { var products = ShoppingContent.Products.list(merchantId, { pageToken: pageToken, maxResults: maxResults }); Logger.log('Page ' + pageNum); if (products.resources) { for (var i = 0; i < products.resources.length; i++) { res.push({id: products.resources[i].id, title: products.resources[i].title, price: products.resources[i].price.value}); // Added Logger.log('Item [' + i + '] (product price) ==> ' + products.resources[i].price.value); // or products.resources[i].price.currency } } else { Logger.log('No more products in account ' + merchantId); } pageToken = products.nextPageToken; pageNum++; } while (pageToken); Logger.log(res); // Added }
注:
- 在这个答案中,假设您已经能够使用
ShoppingContent.Products.list()
方法。请注意这一点。