Javascript 如何使用getRange选择非相邻行?
我想从源工作表复制范围A1、C1:Z1、A3、C3:Z3、A6、C6:Z6,并将这些单元格的值粘贴到目标工作表,同时删除未选择的单元格,如B列、第2行、第4行和第5行 来源 目标Javascript 如何使用getRange选择非相邻行?,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我想从源工作表复制范围A1、C1:Z1、A3、C3:Z3、A6、C6:Z6,并将这些单元格的值粘贴到目标工作表,同时删除未选择的单元格,如B列、第2行、第4行和第5行 来源 目标 我相信你的目标如下 您希望使用Google Apps脚本将背景颜色突出显示的单元格值从源工作表复制到目标工作表。 流量: 从源工作表中检索背景颜色和值。 使用检索到的背景颜色和值创建用于复制到目标图纸的数组。 将创建的数组放入目标工作表。 示例脚本1: 在此示例中,将从示例输入和输出图像中检索背景颜色高亮显示的单元格
我相信你的目标如下 您希望使用Google Apps脚本将背景颜色突出显示的单元格值从源工作表复制到目标工作表。 流量: 从源工作表中检索背景颜色和值。 使用检索到的背景颜色和值创建用于复制到目标图纸的数组。 将创建的数组放入目标工作表。 示例脚本1: 在此示例中,将从示例输入和输出图像中检索背景颜色高亮显示的单元格的值。在使用此脚本之前,请设置sourceSheetName、destinationSheetName和backgroundColor的变量 示例脚本2: 在此模式中,例如,在示例输入情况下,即使单元格C3和A6为默认背景色,脚本也会通过删除具有默认背景色的列来复制高亮显示单元格中的值
function myFunction() {
var sourceSheetName = "Sheet1"; // Please set the source sheet name.
var destinationSheetName = "Sheet2"; // Please set the destination sheet name.
var backgroundColor = "###"; // Please set the background color you want to check.
// 1. Retrieve the background colors and values from the source sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName(sourceSheetName);
var range = srcSheet.getDataRange();
var backgroundColors = range.getBackgrounds();
var values = range.getValues();
// 2. Create an array for copying to the target sheet using the retrieved background colors and values.
var tempValues = backgroundColors.reduce((ar1, r, i) => {
var temp = r.reduce((ar2, c, j) => {
ar2[j] = c == backgroundColor ? values[i][j] : "";
return ar2;
}, []);
if (temp.length > 0 && temp.some(e => e.toString() != "")) {
ar1.push(temp);
}
return ar1;
}, []);
var obj = tempValues.reduce((o, r, i) => {
r.forEach((c, j) => {
if (c.toString() == "" && i == 0) o[j] = true;
if (c.toString() != "" && o[j] && i > 0) delete o[j];
});
return o;
}, {});
var copyValues = tempValues.map(r => r.filter((_, j) => !obj[j]));
// 3. Put the created array to the target sheet.
var dstSheet = ss.getSheetByName(destinationSheetName);
dstSheet.getRange(1, 1, copyValues.length, copyValues[0].length).setValues(copyValues);
}
注:
在您的示例脚本中,我根据您的输入情况进行了准备。因此,当实际情况与示例情况不同时,脚本可能无法工作。所以请小心这个
如果要复制除默认背景色ffffff以外的单元格值,请按如下方式修改上述脚本
从
致:
或
从
致:
当源工作表和标记工作表的电子表格不同时,请修改var ss=SpreadsheetApp.getActiveSpreadsheet
参考资料:
使用getRange检索所有范围,并使用
//模拟值
常量源表={
getDataRange:=>{
getValues:=>
[…新阵列5]。地图
i=>=>newarray10.fill.map=>++i0
,
},
};
//模拟结局
常量值=sourceSheet.getDataRange.getValues;
const rowIndexToarayIndex=num=>num-1;
const excludeCols=[2]。MapRowIndexToaryIndex;
const excludeRows=[2,4,5].maprowIndexToArrayIndex;
常量filteredValues=值
.filter_行,i=>!排除行
.maprow=>row.filter,j=>!不包括Cols.includesj;
console.info{values,filteredValues};请按照中的建议添加您的搜索/研究工作的简要说明。感谢您的详细阐述!这些亮点仅仅是为了演示,以便于识别我打算转移的单元格:很抱歉在原始帖子中没有明确说明。然而,你的解决方案和解释很有创意,也很容易理解!我敢肯定,在某些情况下,你的方法会在以后的道路上派上用场。再次感谢@谢谢你的回复。很抱歉,我误解了你的问题。从你的回答中,我可以注意到这一点。但从你的回复来看,如果我的样品对用户有用,我很高兴。所以我想留下我的答案。
function myFunction() {
var sourceSheetName = "Sheet1"; // Please set the source sheet name.
var destinationSheetName = "Sheet2"; // Please set the destination sheet name.
var backgroundColor = "###"; // Please set the background color you want to check.
// 1. Retrieve the background colors and values from the source sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName(sourceSheetName);
var range = srcSheet.getDataRange();
var backgroundColors = range.getBackgrounds();
var values = range.getValues();
// 2. Create an array for copying to the target sheet using the retrieved background colors and values.
var tempValues = backgroundColors.reduce((ar1, r, i) => {
var temp = r.reduce((ar2, c, j) => {
ar2[j] = c == backgroundColor ? values[i][j] : "";
return ar2;
}, []);
if (temp.length > 0 && temp.some(e => e.toString() != "")) {
ar1.push(temp);
}
return ar1;
}, []);
var obj = tempValues.reduce((o, r, i) => {
r.forEach((c, j) => {
if (c.toString() == "" && i == 0) o[j] = true;
if (c.toString() != "" && o[j] && i > 0) delete o[j];
});
return o;
}, {});
var copyValues = tempValues.map(r => r.filter((_, j) => !obj[j]));
// 3. Put the created array to the target sheet.
var dstSheet = ss.getSheetByName(destinationSheetName);
dstSheet.getRange(1, 1, copyValues.length, copyValues[0].length).setValues(copyValues);
}
if (c == backgroundColor) ar2.push(values[i][j]);
if (c != "#ffffff") ar2.push(values[i][j]);
ar2[j] = c == backgroundColor ? values[i][j] : "";
ar2[j] = c != "#ffffff" ? values[i][j] : "";
const values = sourceSheet.getDataRange().getValues();
const rowIndexToArrayIndex = num => num - 1;
const excludeCols = [2].map(rowIndexToArrayIndex);
const excludeRows = [2, 4, 5].map(rowIndexToArrayIndex);
const filteredValues = values
.filter((_row, i) => !excludeRows.includes(i))
.map(row => row.filter((_, j) => !excludeCols.includes(j)));
console.info({ values, filteredValues });