我對這些事情很陌生,對這些錯誤感到抱歉。每當其他列中的值為 0 時,我都嘗試使用此腳本從列中洗掉復選框。腳本如下:
function onEdit() {var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2"); //change this to the name of your sheetui = SpreadsheetApp.getUi();
var names = ss.getRange("C1:C");
var namesValues = names.getValues(); //Get array of all the names
var checkboxes = ss.getRange("B1:B");
var cbRows = checkboxes.getHeight(); //Get # of rows in the rangesvar cbValues = checkboxes.getValues(); //Get array of all the checkbox column cell values//Logger.log(cbValues);
var newCBValues = new Array(cbRows); //Create an array to store all the new checkboxes values before we edit the actual spreadsheet
for (var row = 0; row < cbRows; row ) {
newCBValues[row] = new Array(0); // Make the array 2 dimensional (even though it only has 1 column, it must be 2D).
if (namesValues[row] == "0" || namesValues[row] == " ") { //If the name cell of this row is empty or blank then...
newCBValues[row][0] = " "; //Set the value to one space (which will make the cell NOT true or false, and thus NOT display a checkbox).
//Logger.log("newCBValues[" row "][0]: " newCBValues[row][0]);
}else{ //otherwise, if the name cell isn't blank...
if (cbValues[row][0] === true) {
newCBValues[row][0] = true; //Keep the checkbox checked if it's already checked
}else{ //If the name cell isn't blank, and it's not true...
newCBValues[row][0] = false; //Then Keep it or set it to False (an empty checkbox):
}
}
}
checkboxes.setDataValidation(SpreadsheetApp.newDataValidation().requireCheckbox()).setValues(newCBValues);
}
如果在 var names = ss.getRange("C1:C") 我只選擇一列它有效。但是當我想為更多列(例如C1:E)設定它時它不起作用。
希望您能夠幫助我。謝謝!!!!
編輯:
https://docs.google.com/spreadsheets/d/1MjIuZbON_nlaENqyART_UnQZZ-ooZOMLjKmDM-nZl4M/edit#gid=1464332245
這是我正在嘗試的檔案。表 2。您可以從應用程式腳本中看到如果我寫的是 (C1:E) 而不是 var names = ss.getRange ("C1:C") 會發生什么變化。自己嘗試一下(查看復選框列的差異)。謝謝!!!!
編輯2:
這是輸入(在運行腳本之前)
This is what I want (cell with 1 to have the checkboxes and cell with 0 not to have it)
uj5u.com熱心網友回復:
從您提供的示例輸入和輸出情況來看,我相信您的目標如下。
- 當“A”和“B”列是 時,您希望將復選框放在“D”列
1。
在這種情況下,下面的示例腳本怎么樣?根據您的顯示腳本,在您的情況下,您可能希望通過腳本編輯器運行腳本。所以在這個修改中,沒有使用事件物件。
示例腳本:
function onEdit() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var values = sheet.getRange("A1:B" sheet.getLastRow()).getValues();
var rangeList = values.map((r, i) => r.every(e => e == 1) ? `D${i 1}` : "").filter(String);
sheet.getRangeList(rangeList).insertCheckboxes();
}
筆記:
- 此示例腳本適用于您提供的示例電子表格。因此,當您更改電子表格時,可能無法使用該腳本。請注意這一點。
參考:
- 地圖()
- getRangeList(a1Notations)
添加:
當我問我的理解In your goal, when the values of the columns "A" and "B" are 1, you want to put the checkbox to the column "D". Is my understanding correct?是否正確時,你說Exactly!!!。但是,根據您的以下回復,
這對我的示例非常有用,但正如您所說,它不適用于我的原始檔案。我更改了作業表中的一些值,使其與我實際擁有的更相似
我想在 B 上添加復選框,其中 C、D 和 E 有 1
如果要在“C”、“D”和“E”列時放置復選框1,示例腳本如下。
示例腳本:
function onEdit() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var lastRow = sheet.getLastRow();
var values = sheet.getRange("C1:E" lastRow).getValues();
sheet.getRange("B1:B" lastRow).removeCheckboxes();
var rangeList = values.map((r, i) => r.every(e => e == 1) ? `B${i 1}` : "").filter(String);
sheet.getRangeList(rangeList).insertCheckboxes();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/450842.html
標籤:arrays google-apps-script google-sheets checkbox script
