我正在尝试将突出显示的单元格移动到列表底部,然后整理列表。这是我目前所拥有的。
function duplicateAndOrganizeActiveSheet() {
// Duplicate the sheet
var mySS = SpreadsheetApp.getActiveSpreadsheet();
var duplicateSheet = mySS.duplicateActiveSheet();
// Change the name of the new sheet.
duplicateSheet.setName(duplicateSheet.getSheetId());
duplicateSheet.setTabColor(null);
// Get the array of background color in each cell for that range
var range = duplicateSheet.getRange('A2:A')
var bkg = range.getBackgrounds();
// Take highlighted cells and move them to the bottom of the list, then tidy the list
// iterate over rows
for(var i=0;i<bkg.length;i++){
// iterate over columns
for(var j=0;j<bkg[i].length;j++){
// if the specific cell at row i and column j is mid yellow '#ffd966'
var results = [[],[]]
if(bkg[i][j]==='#ffd966') results.push([i],[j])
// Move matching cells to the bottom of the list
var end_list = duplicateSheet.getRange('A:A').getNextDataCell(SpreadsheetApp.Direction.DOWN)
var destination = duplicateSheet.getRange(end_list +':A')
destination.setValues(results)
}
}
// trim blank cells from top of list
var range2 = duplicateSheet.getRange('E2:E'+duplicateSheet.getLastRow()) // get a range start from row 2
var data = range2.getValues().filter(String); // get a data and remove empty elements
range2.clearContent().offset(0,0,data.length).setValues(data); // put the data back on the sheet
循环序列之外的所有内容都按预期工作,我只是没有足够的经验来使输出正常工作。
我开始于
var results = [] if(bkg[i][j]==='#ffd966') results.push(i,j)
然后用谷歌搜索错误消息,然后再次尝试,然后用谷歌搜索下一条错误消息,等等...
我当前的错误消息是 例外:第 21 行未找到范围(results.push)
这是我的一位 Works 朋友提出的解决方案。
function transferData() {
// Step 1: Count cells that are not empty in range C2 - C8
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Baylands DM Schedual");
var range = sourceSheet.getRange("C2:C8");
var values = range.getValues();
var count = values.reduce(function(acc, val) {
if (val[0] !== "") {
return acc + 1;
} else {
return acc;
}
}, 0);
// Step 2: Go to sheet called history and insert number of rows equal to count from step 1, starting from row 2
var historySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("History");
historySheet.insertRowsAfter(1, count);
// Step 3: Return to sheet called Baylands DM Schedual and copy content including cell color cells A2, B2, and C (count from step 1)
var sourceRange = sourceSheet.getRange("A2:C" + (count + 1));
var sourceValues = sourceRange.getValues();
var sourceBackgrounds = sourceRange.getBackgrounds();
// Step 4: Return to sheet called history and paste cells into row 2
var historyRange = historySheet.getRange("A2:C" + (count + 1));
historyRange.setValues(sourceValues);
historyRange.setBackgrounds(sourceBackgrounds);
// Step 5: Return to sheet called Baylands DM Schedual and sort items in cell range C (count from step 1)
var sortRange = sourceSheet.getRange("C2:C" + (count + 1));
var colorSortValues = [];
var noColorSortValues = [];
for (var i = 0; i < count; i++) {
var cellColor = sortRange.getCell(i + 1, 1).getBackground();
if (cellColor !== "#ffffff") { // Check if cell color is not white (no color)
colorSortValues.push([i + 1, sortRange.getCell(i + 1, 1).getValue()]);
} else {
noColorSortValues.push([i + 1, sortRange.getCell(i + 1, 1).getValue()]);
}
}
if (colorSortValues.length > 0 && noColorSortValues.length > 0) {
// If there are cells with color and cells without color, sort them accordingly
colorSortValues.sort(function(a, b) { return a[1] - b[1]; });
noColorSortValues.sort(function(a, b) { return a[1] - b[1]; });
var sortedValues = noColorSortValues.concat(colorSortValues);
var sortedRange = sourceSheet.getRange("C2:C" + (count + 1));
sortedRange.clearContent();
for (var j = 0; j < sortedValues.length; j++) {
sortedRange.getCell(j + 1, 1).setValue(sortedValues[j][1]);
}
}
// Step 6: Clear cells A2 and B2
sourceSheet.getRange("A2:B2").clear();
}