我希望將以特定顏色突出顯示的單元格復制到一列中的不同作業表中。
示例檔案:https ://docs.google.com/spreadsheets/d/1Xa_WKlmHO5mT688zM0O-LXlqnSbITG2YK1uLPq5XzcA/edit#gid=328987545
您能幫我撰寫腳本以獲得所需的輸出嗎?
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想要檢索突出顯示的單元格的值(從您提供的電子表格中,它是
#ffff00),并希望將這些值放入目標作業表的“A”列。 - 您想使用 Google Apps 腳本實作此目的。
在這種情況下,下面的示例腳本怎么樣?
示例腳本:
function myFunction() {
const srcSheetName = "Sheet1"; // This is from your sample Spreadsheet.
const dstSheetName = "Ouput"; // This is from your sample Spreadsheet.
const checkColor = "#ffff00"; // This is from your sample Spreadsheet.
// Retrieve sheets.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const [src, dst] = [srcSheetName, dstSheetName].map(s => ss.getSheetByName(s));
// Retrieve source values and backgrounds.
const srcRange = src.getDataRange();
const backgrounds = srcRange.getBackgrounds();
// Create an array for putting to the destination sheet.
const values = srcRange.getValues().reduce((ar, r, i) => {
r.forEach((c, j) => {
if (backgrounds[i][j] == checkColor) {
ar.push([c]);
}
});
return ar;
}, []);
// Put the array to the column "A" of the destination sheet.
dst.getRange(1, 1, values.length).setValues(values).setBackground(checkColor);
}
- 運行此腳本時,將從源作業表突出顯示的單元格中檢索值。并且,將檢索到的值放入目標作業表的“A”列。
- 在您的示例電子表格中,目標作業表具有突出顯示的單元格。所以,我添加了
setBackground(checkColor). 如果您不想設定顏色,請將其洗掉。
參考:
- 獲取背景()
- 獲取值()
- 設定值(值)
- 減少()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/488102.html
上一篇:已解決-使用Google腳本在Google表格之間復制行
下一篇:計算表中兩個日期之間的金額
