[英]Google Apps Script If Statement Matches Cell Value
I have a query regarding Google App Script and If Statements. 我有一个有关Google App脚本和If语句的查询。
I have pushed data from my Ads account to a Google sheet. 我已将广告帐户中的数据推送到Google表格中。 I then want multiple sheets to pull specific data from that.
然后,我希望多个工作表从中提取特定数据。 So I am trying to make one sheet the source of all data.
因此,我试图将一张表作为所有数据的来源。 I can then select parts of the data I need for each of my report.
然后,我可以为每个报告选择所需的部分数据。
The main one is to get the range where the day of the week == today. 主要的是获得星期几==今天的范围。
I have tried doing this by adding a check variable which is then passed into a for loop checking if the day == the dayName variable. 我尝试通过添加一个检查变量来执行此操作,然后将该检查变量传递到for循环中,以检查day == dayName变量。
I am currently getting an execution issue with it timing out. 我目前遇到执行超时问题。 This is leading me to believe I have an issue with the way I have done this.
这使我相信我的操作方式存在问题。
The data set is 21k rows: A is date, B is day of week, C is campaign and so on up to column I. 数据集为2万1千行:A是日期,B是星期几,C是活动,依此类推,直到第I列。
I'm still very new to the scripting game and any help is truly appreciated. 我仍然对脚本游戏还很陌生,对您的帮助深表感谢。
The reason I am looking to do it this way is because I seriously struggled to include a where statement within ads script to filter by DayOfWeek. 我之所以要这样做,是因为我非常努力地在广告脚本中加入一个where语句,以便按DayOfWeek进行过滤。 I then thought this may an alternate solution.
然后,我认为这可能是替代解决方案。
Thanks, Liam 谢谢,利亚姆
function myFunction() {
//This is the days of the week for the today lookup
var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
//This is today's date
var end = new Date().toISOString().slice(0,10);
//This supplies the current day of week
var dow = new Date(end);
var dayName = days[dow.getDay()];
var sss = SpreadsheetApp.openById('SheetID'); //ID of the sheet
var ss = sss.getSheetByName('googleData'); //Sheet name
var range = ss.getRange('A1:I'); //The range to copy
var check = ss.getRange('B2:B').getValues(); The range to if statement against
var data = range.getValues();
var tss = SpreadsheetApp.openById('SheetID'); //destination sheet ID
var ts = tss.getSheetByName('usedData'); //Destination sheet name
for (var i=0;i<check.length; i++) {
if(check[i]==dayName) {
ts.getRange(1, 1, data.length, data[0].length).setValues(data); //you will need to define the size of the copied data see getRange()
}
}
}
I believe what you want is to find all rows that match the current day of week and copy to userData. 我相信您想要的是找到与当前星期几匹配的所有行并将其复制到userData。 Try this.
尝试这个。
function myFunction() {
try {
// javascript getDay starts with Sunday
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var today = new Date();
var dayOfWeek = days[today.getDay()];
// Do you need id or just active spreadsheet?
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("googleData");
// Get all data columns A thru I
var data = sheet.getRange(1,1,sheet.getLastRow(),9).getValues();
// results will become a 2D array to use setValues
var results = [];
for( var i=0; i<data.length; i++ ) {
// Day of week in column B
if( data[i][1] === dayOfWeek ) results.push(data[i]);
}
sheet = spread.getSheetByName("usedData");
sheet.clear();
sheet.getRange(1,1,results.length,results[0].length).setValues(results);
}
catch(err) {
Logger.log(err);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.