gpt4 book ai didi

Javascript:在插入 Google 表格之前按月而不是按天对 csv 数据进行分组

转载 作者:行者123 更新时间:2023-11-28 04:10:43 25 4
gpt4 key购买 nike

我对 Javascript 很陌生,但最近开始修改脚本,以便我们使用 Google Apps 脚本将各种 csv 报告提取到 Google 表格中。我们当前的脚本读取 csv 文件,然后在第 8 行之后插入所有数据(因为该行之前的行仅包含生成时间、报告名称等信息)。

现在的挑战是,我们需要一份按月拆分为表格的年初至今的报告(如果我们按天拆分,则会达到 200 万个单元格的限制)。但由于本例中的报告工具不允许按除日以外的任何其他日期变量进行分段,因此我们必须在脚本内进行拆分。因此,我们基本上会发送一份年初至今的报告,其中包含按天划分的广告级数据。然后,在将数据插入表格之前,会在脚本中按月而不是按日对广告级数据进行分组。换句话说,我想要例如广告的费用、展示次数和点击数据将在一个月内每天发生的所有事件进行汇总。

过去几天我进行了深入的研究,但尚未找到针对此特定问题的有效解决方案。如果有人想看看这个,我将非常感激。

以下两个脚本供您引用。第一个是我们当前的工作脚本,它只插入第 8 行的 csv 数据(但没有分组):

 function importData() {
var sheet_atlas = SpreadsheetApp.getActive().getSheetByName('sheet_name');
sheet_atlas.getRange('A2:V10000').clearContent();
var sheetName = "sheet_name"

var threads = GmailApp.search("attachment_name")
var msgs = GmailApp.getMessagesForThreads(threads);
var newData = [];

for (var i = 0 ; i < msgs.length; i++) {
for (var j = 0; j < msgs[i].length; j++) {
var attachments = msgs[i][j].getAttachments();
var bodyEmail = msgs[0][0].getBody();

var regExp = new RegExp('a href="(.*?)"', "gi");
//var regExp = new RegExp('data-saferedirecturl="(.*?)"', "gi"); // "i" is for case insensitive
var url = regExp.exec(bodyEmail)[1];
Logger.log(url)
var decode = new XML('<d>' + url + '</d>');
var strDecoded = decode.toString()
var response = UrlFetchApp.fetch(strDecoded).getContentText();
var csvdata = Utilities.parseCsv(response)
var newOrders = []
//Logger.log(csvdata)
for (var eachRow in csvdata){
//for(var col1 in csvdata[8]){
//if(csvdata[8][col1] == 'Campaign Name'){ var campaignCol = col1 }

//else if(csvdata[8][col1] == 'Publisher Name'){ var publisherCol = col1 }

//else if(csvdata[8][col1] == 'Statistics Date'){ var dateCol = col1 }

//}
//Logger.log(dateCol)
//Logger.log(publisherCol)
//Logger.log(campaignCol)

if (eachRow>8)
{
var theRow = []
theRow.push(csvdata[eachRow][0])
theRow.push(csvdata[eachRow][1])
theRow.push(csvdata[eachRow][2])
theRow.push(csvdata[eachRow][3])
theRow.push(csvdata[eachRow][4])
theRow.push(csvdata[eachRow][5])
theRow.push(csvdata[eachRow][6])
theRow.push(csvdata[eachRow][7])
theRow.push(csvdata[eachRow][8])
theRow.push(csvdata[eachRow][9])
theRow.push(csvdata[eachRow][10])
theRow.push(csvdata[eachRow][11])
theRow.push(csvdata[eachRow][12])
theRow.push(csvdata[eachRow][13])
theRow.push(csvdata[eachRow][14])
theRow.push(csvdata[eachRow][15])
theRow.push(csvdata[eachRow][16])
theRow.push(csvdata[eachRow][17])
theRow.push(csvdata[eachRow][18])
theRow.push(csvdata[eachRow][19])
theRow.push(csvdata[eachRow][20])
theRow.push(csvdata[eachRow][21])
newOrders.push(theRow)
}
}
}

}
Logger.log(newOrders)
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(2,1, newOrders.length, newOrders[0].length).setValues(newOrders)

}

第二个是我当前创建分组的尝试。到目前为止,我已经成功地突破了这个月,但是当涉及到总结部分时,一切都出了问题。

function importData() {
var sheet_atlas = SpreadsheetApp.getActive().getSheetByName('sheet name');
sheet_atlas.getRange('L2:AA60000').clearContent();
var sheetName = "sheet name"

var threads = GmailApp.search("attachment_name")
var msgs = GmailApp.getMessagesForThreads(threads);
var newData = [];

for (var i = 0 ; i < msgs.length; i++) {
for (var j = 0; j < msgs[i].length; j++) {
var attachments = msgs[i][j].getAttachments();
var bodyEmail = msgs[0][0].getBody();

var regExp = new RegExp('a href="(.*?)"', "gi");
var url = regExp.exec(bodyEmail)[1];
Logger.log(url)
var decode = new XML('<d>' + url + '</d>');
var strDecoded = decode.toString()
var response = UrlFetchApp.fetch(strDecoded).getContentText();
var csvdata = Utilities.parseCsv(response)
var newOrders = []
//Logger.log(csvdata)
var currMonth = "";
var theRow = [];
var columns = 11;
var formattedMonth = "";
for (var eachRow in csvdata){
//for(var col1 in csvdata[8]){
//if(csvdata[8][col1] == 'Campaign Name'){ var campaignCol = col1 }

//else if(csvdata[8][col1] == 'Publisher Name'){ var publisherCol = col1 }

//else if(csvdata[8][col1] == 'Statistics Date'){ var dateCol = col1 }

//}
//Logger.log(dateCol)
//Logger.log(publisherCol)
//Logger.log(campaignCol)

if (eachRow>8)
{
var date = csvdata[eachRow][2].split("-")
var month = date[0]
if((currMonth != "") && (month.localeCompare(currMonth) != 0)) {
theRow[2] = formattedMonth
newOrders.push(theRow)
theRow = []
currMonth = month
for (var ci = 0; ci < columns; ci++){
// Reset data structure
theRow.push("0")
}
}

formattedMonth = date[0] + "-" + date[2]

for(var cy = 0; cy < columns; cy++) {
if(cy != 2){
// Sum the columns
theRow[cy] = parseFloat(theRow[cy]) + parseFloat(csvdata[eachRow][cy])
}
}
}
}
// Pushing final row
Logger.log(formattedMonth)
newOrders.push(theRow)

}
Logger.log(newOrders)
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(2,12, newOrders.length, newOrders[0].length).setValues(newOrders)

}
}

提前致谢,如果有任何问题需要澄清,请告诉我!

最佳答案

这不是答案,因为您没有提供足够的信息,而是一些关于可能出现问题的地方的提示。

你确实应该使用分号来结束表达式,无论是谁留给你维护这段代码,都需要提醒他们对他人的义务。

代码中的一个问题是 currMonth 是一个空字符串:

var currMonth = "";

...

if (eachRow > 8)
{
var date = csvdata[eachRow][2].split("-")
var month = date[0]

// This expression must always return false since currMonth == ""
if((currMonth != "") && (month.localeCompare(currMonth) != 0)) {
theRow[2] = formattedMonth
newOrders.push(theRow)
theRow = []

// this statement expression will never be executed,
// so currMonth remains an empty string
currMonth = month
for (var ci = 0; ci < columns; ci++){
// Reset data structure
theRow.push("0")
}
}

同时查看:

for (var eachRow in csvdata){

if (eachRow>8)

大概 eachRow 是一个字符串,如果 Number(eachRow) 不返回 a,则使用 > 将其与数字进行比较将始终返回 false数字(例如,如果 eachRow 是“12”或“6”等字符串,但不是“2017-08-28”等字符串,它将正确计算)。

您需要提供csvdata的示例以及您期望处理它的输出。

关于Javascript:在插入 Google 表格之前按月而不是按天对 csv 数据进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46297912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com