我試圖比較 2 列 B 和 C,如果結果是 D 列上的不同設定值
示例:https ://docs.google.com/spreadsheets/d/1Y7--aDDUwSR08GtdakQIFT9QPGWGILHSaHgSdh6jsmU/edit#gid=788248808
function COMPARE()
{
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ss_s = ss.getSheetByName('temp');
const LR = ss_s.getLastRow();
const B = ss_s.getRange("B2:B" LR).getValues();
const C = ss_s.getRange("C2:C" LR).getValues();
var Data = [];
for (var i = 0; i < LR; i )
{
if (B[i] !== C[i])
{
Data.push([B[i]]);
}
else
{
Data.push([""]);
}
ss_s.getRange(2,4,LR,1).setValues(Data);
}
// if( B !== C )
// {
// var Data = ss_s.getRange(2,2,LR,1).getValues();
// ss_s.getRange(2,4,LR,1).setValues(Data);
// }
};
錯誤
Exception: The number of rows in the data does not match the number of rows in the range. The data has 1 but the range has 7.
COMPARE @ compare.gs:22
我不完全理解,因為我試圖遵循我在周圍看到的答案和例子,到目前為止沒有太多成功
uj5u.com熱心網友回復:
從您的腳本中,我理解如下。
- 當“B”和“C”列的值相同時,您要放入
""“D”列。當“B”列和“C”列的值不同時,您希望將“B”列的值放到“D”列。
如果我的理解是正確的,那么下面的修改呢?
修改后的腳本:
當你的腳本被修改后,它變成如下。getValues()回傳一個二維陣列。在您的腳本中,為了使用您的 for 回圈,請將B和的值變平C。而且,請移到ss_s.getRange(2, 4, LR, 1).setValues(Data);回圈外。我認為這些是您錯誤的原因。
function COMPARE() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ss_s = ss.getSheetByName('temp');
const LR = ss_s.getLastRow();
const B = ss_s.getRange("B2:B" LR).getValues().flat();
const C = ss_s.getRange("C2:C" LR).getValues().flat();
var Data = [];
for (var i = 0; i < LR; i ) {
if (B[i] !== C[i]) {
Data.push([B[i]]);
} else {
Data.push([""]);
}
}
ss_s.getRange(2, 4, LR, 1).setValues(Data);
}
或者,您也可以進行如下修改。
function COMPARE2() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ss_s = ss.getSheetByName('temp');
const values = ss_s.getRange("B2:C" ss_s.getLastRow()).getValues();
const data = values.map(([b, c]) => [b == c ? "" : b]);
ss_s.getRange(2, 4, values.length).setValues(data);
}
參考:
- 獲取值()
添加:
從下面的回復中,
確實該宣告是錯誤的并且錯過了公寓(我不知道)但是您的解決方案只是將值設定為已更改的內容,其他所有內容都是“”,Data.push([“”]實際上是此示例的冗余如果任何值發生變化,我只想設定值,并保持不變,基本上將 B 列的 valueS 放入 D 列
而且,關于我的確認When the columns "B" and "C" are edited, you want to compare the column "B" and "C" of the edited row, and when the value of column "B" is different from the column "C", you want to put the value of column "B" to the column "D". Is my understanding correct?,您說如下。
是的,@Tanaike 你理解正確,如果 B 與 C 相比更改了任何行值,則將 B 復制到 D
在這種情況下,我認為為了實作您的目標,需要使用 OnEdit 觸發器,如下所示。
示例腳本:
function onEdit(e) {
const range = e.range;
const sheet = range.getSheet();
const row = range.rowStart;
if (sheet.getSheetName() != 'temp' || row == 1 || !(range.columnStart == 2 || range.columnStart == 3)) return;
const [b, c] = sheet.getRange(row, 2, 1, 2).getValues()[0];
sheet.getRange(row, 4).setValue(b == c ? null : b);
}
When you use this script, please edit the cells "B2:C" of "temp" sheet. When the script is run and the values of columns "B" and "C" are comparead and when the values are different, the value of column "B" is put to the column "D".
If you want to run the script only when the column "B" is edited, please modify
if (sheet.getSheetName() != 'temp' || row == 1 || !(range.columnStart == 2 || range.columnStart == 3)) return;toif (sheet.getSheetName() != 'temp' || row == 1 || range.columnStart != 2) return;.When you directly run this script, an error like
TypeError: Cannot read property 'range' of undefinedoccurs. Because this script is run by the OnEdit trigger. Please be careful this.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438802.html
