我想根据同一行中另一个单元格的值将单元格格式化为自定义数字格式。例如,在范围A1:U1000
中,我想根据单元格J1
的文本值将单元格K1
和G1
格式化为相同的自定义数字格式。我需要将其向下传播几千行。我编辑了script,发现它允许我设置当前选择的单元格的自定义数字格式,但是我想让它在编辑G
列中的值时运行。我无法将其合并到onEdit()
函数中。
function testV(){
setNumberFormat('Volume');
}
function testM(){
setNumberFormat('Mass');
}
function setNumberFormat(format) {
var range = SpreadsheetApp.getActiveRange();
var numberFormat = '';
try {
switch (format){
case 'Volume':
numberFormat = '##0.00,# in³';
break;
case 'Mass':
numberFormat = '##0.00,# g';
break;
}
range.setNumberFormat(numberFormat);
} catch (e){
throw new Error('There was an error: ' + e);
}
}
答案 0 :(得分:0)
我能够在原始代码的基础上实现所需的结果。它确实对我有用,但是似乎效率不高。我正在格式化的单元格需要一段时间才能更新。
var app = SpreadsheetApp;
var activeSheet = app.getActiveSpreadsheet().getActiveSheet();
var someCell = activeSheet.getActiveRange().getValue();
var someRow = activeSheet.getActiveRange().getRowIndex();
var someColumn = activeSheet.getActiveRange().getColumn();
function onEdit() {
if (someCell == 'Machine 1' && someColumn == '7'){
setNumberFormat('Mass');
}
else if (someCell == 'Machine 2' && someColumn == '7') {
setNumberFormat('Mass');
}
else if (someCell == 'Machine 3' && someColumn == '7') {
setNumberFormat('Mass');
}
else if (someCell == 'Machine 4' && someColumn == '7') {
setNumberFormat('Volume');
}
else if (someCell == 'Machine 5' && someColumn == '7') {
setNumberFormat('Sheet');
}
else if (someCell == 'CAD' && someColumn == '7') {
setNumberFormat('N/A');
}
else if (someCell == '' && someColumn == '7'){
setNumberFormat('Blank')
}
}
function setNumberFormat(format) {
var rangeMaterial = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(someRow,10);
var rangeSupport = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(someRow,11);
var numberFormat = '';
try {
switch (format){
case 'Volume':
numberFormat = '##0.00,# in³';
break;
case 'Mass':
numberFormat = '##0.00,# g';
break;
case 'Sheet':
numberFormat = '##,# "sheets"';
break;
case 'N/A':
numberFormat = '##,# "N/A"';
break;
case 'Blank':
numberFormat = '##,#';
break;
}
rangeMaterial.setNumberFormat(numberFormat);
rangeSupport.setNumberFormat(numberFormat);
} catch (e){
throw new Error('There was an error: ' + e);
}
}
答案 1 :(得分:0)
我为类似的post创建了一个触发器,可以在这里重用。试试这个...
function onEdit(e) {
// set the range to monitor for edits
var editRange = {
top : 5, bottom : 20, // row numbers in search range
left : 7, right : 7 // column numbers in search range
};
var thisRow = e.range.getRow(); // find the row number that was changed
if(thisRow < editRange.top || thisRow > editRange.bottom) return; // exit if not in these rows
var thisColumn = e.range.getColumn(); // find the column number that was edited
if(thisColumn < editRange.left || thisColumn > editRange.right) return; // exit if not in these columns
var shtIn = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("stock"); // set the sheet for the cell that will be formatted
var numberFormat = '';
switch(e.value) {
case 'Mass':
numberFormat = '##0.00,# g';
break;
case 'Volume':
numberFormat = '##0.00,# in³'
break;
}
shtIn.getRange(thisRow, 10).setNumberFormat(numberFormat);
shtIn.getRange(thisRow, 11).setNumberFormat(numberFormat);
}
这里是指向sample sheet的链接,希望该链接可以按您期望的方式工作。