所以我还在学习Javascript并试图找到自己的方式。 我正在尝试一些可以节省我每周工作时间的工作。
这是googlespread表的脚本。它的目的是在列中查找键值并相应地移动数据。然后,它应该按区域将数据拆分为外部电子表格,并将其分成多个标签。
我认为我的分割位是正确的,但它似乎不起作用的是判断应该移动什么,不应该移动什么。我很确定我的While循环和while循环中的if语句可能有问题:(
如果有人能指出我的方式错误,我将非常感激。这是完整的脚本。
应该移动的唯一项目如下。 IF列AV =需要删除详细信息和列AW不为空白且列AX不为空白。 AND列BC不是空白。然后它应该被推送归档。
OR
如果AV为空并且aw不为空并且bc不为空白。那么这也应该被推送归档。
非常感谢
function testNinja(){
var sourceSheet = "Change of details requests";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var values = sheet.getDataRange().getValues();
var valuesLength = values.length;
var archive = [];
var counter = 1;
var agentSheet = "ID HERE";
// To make things easier, I've defined the worksheet names and the region names into arrays. Position is important, as entry 0 matches entry 0
var regionSheets = ["R/North East&Yorks","R/North West","R/South East","D/South London","D/East","D/North London","M/Midlands","M/Wales&S.West","M/Scotland/N.Ireland"];
var regionNames = ["North East & Yorks","North West","South East","South London","East England","North London","Midlands","Wales & S West","Scotland & N Ireland"];
// giving key columns & key values a var to save typing. Using column letters as var name.
var av = values[counter][47];
var aw = values[counter][48];
var ax = values[counter][49];
var bc = values[counter][54];
var bf = values[counter][57];
var bk = "Need to Remove Details";
var sf = "SET SF TASK";
// put matching values into an archive. This bit doesn't seem to be working correctly
while(counter < valuesLength)
{
if (bf == sf)
{
counter++;
}
if(av == bk && aw != "" && ax != "" && bc != "" || av == "" && aw != "" && bc !="")
{
archive.push(values.splice(counter, 1)[0]);
}
else
{
counter++;
}
}
// create a function called 'move' to move columns into new positions
Array.prototype.move = function (old_index, new_index) {
if (new_index >= this.length) {
var k = new_index - this.length;
while ((k--) + 1) {
this.push(undefined);
}
}
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
}
//go through the archive, use the move function, and trim the end columns. Finally add a timestamp
var timestamp = new Date();
for (var i = 0;i<archive.length;i++)
{
archive[i].move(68,5);
archive[i].length = 73;
archive[i][61] = timestamp;
}
// Split the data by region. There is a region value in Column 65. This determinds what Sheet data is moved too.
var regionData = [];
var regionSs = SpreadsheetApp.openById(agentSheet);
for(var i = 0; i < regionSheets.length;i++){
var regionSheet = regionSs.getSheetByName(regionSheets[i]);
var regionName = regionNames[i];
for(var j = 0; j < archive.length;j++){
var value = archive[j][66];
if(value == regionName){
regionData.push(archive.splice(j, 1)[0]);
}
}
//Write data to sheet here.
var regionDataLength = regionData.length;
if (!regionDataLength) continue;
var lastRow = regionSheet.getLastRow();
var requiredRows = lastRow + regionDataLength - regionSheet.getMaxRows();
if (requiredRows > 0) regionSheet.insertRowsAfter(lastRow, requiredRows);
regionSheet.getRange(lastRow + 1, 1, regionDataLength, regionData[0].length).setValues(regionData);
// clear array for new region data
regionData = [];
// Get data validation from CC2, then apply to CC2:CC .
var CCrule = regionSheet.getRange("CC2").getDataValidation();
regionSheet.getRange("CC2:CC").setDataValidation(CCrule);
}
}
答案 0 :(得分:4)
var counter = 1;
//...
var bf = values[counter][57];
//...
var sf = "SET SF TASK";
//...
while(counter < valuesLength)
{
if (bf == sf)
{
counter++;
}
//...
我认为问题是对变量bf
(和其他人)的赋值。您正在将values[1][57]
分配给bf
,因此当计数器增加时,您实际上并没有遍历列(如果values[1][57] != sf
,它将永远不会。)
因此,如果你想使用列标识符来保存输入,我认为你需要在while循环中分配值,而不是在它之前。
答案 1 :(得分:1)
尝试使用此if
:
if ((av == bk && aw != "" && ax != "" && bc != "") || (av == "" && aw != "" && bc !=""))
您必须在逻辑块周围加上括号,否则计算机无法知道如何对条件进行分组,在这种情况下,结果取决于运算符优先级。
编辑:我错了,在JS中&&
的优先级高于||
,所以编程方式它不会改变任何内容,但是这是清晰度的最佳做法以及放置括号的代码的可读性。