[英]Filtering data in custom connector for DataStudio
我正在嘗試創建一個連接到 WooCommerce rest API 的自定義datastudio
連接器。我想區分registered user
下的訂單和guest user
下的訂單。
WooCommerce API 給我custumer_id
字段,如果customer_id = 0
,訂單是由guest
下的,否則user is registered
。
我遵循了谷歌數據工作室教程: https://developers.google.com/datastudio/connector/get-started
這是我的responseToRow
function:
/**
Parse the response data given the required fields of the config
@return the parsed data
*/
function responseToRows(requestedFields, response) {
// Transform parsed data and filter for requested fields
return response.map(function(dailyDownload) {
var row = [];
requestedFields.asArray().forEach(function (field) {
switch (field.getId()) {
case 'id_order':
return row.push(dailyDownload.id);
case 'total':
return row.push(dailyDownload.total);
case 'date_created':
return row.push(dailyDownload.date_created);
case 'registered_user' :
if(parseInt(dailyDownload.customer_id) > 0)
return row.push(dailyDownload.customer_id);
case 'guest_user' :
if(parseInt(dailyDownload.customer_id) == 0)
return row.push(dailyDownload.customer_id);
default:
return row.push('');
}
});
return { values: row };
});
}
function 與教程中給出的類似,其他字段工作正常。 當 customer_id 不同於 0 時,我才返回。它似乎有效,但當條件不成立時,我得到 null 值。
我想刪除 null 值,只有當customer_id
為 0 時右側的訂單和左側補碼相同的訂單。
謝謝您的幫助
我會在填充行數組后嘗試這樣做:
row.forEach((element, index) => {
if(element.guest_user == null){
// To remove the row-entry with guest_user==null
row[index].slice();
}
})
如果您不想從數組中刪除條目,只需創建一個新數組並使用 newArray.push(element); 將 null 復制到其中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.