如何将此简单单元格公式更改为将应用于工作表的脚本:
=if(D3="Devis";B3+8;(if(D3<>"Devis";"")))
其中:
非常感谢
答案 0 :(得分:0)
首先,您需要能够触发脚本的内容。因此,您需要一些简单的onEdit()
函数或更改触发器。
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName('Sheet1');
var valueOfCellEdited = e.value;
Logger.log('valueOfCellEdited: ' + valueOfCellEdited);
var columnOfCellEdited = e.range.getColumn();
Logger.log('columnOfCellEdited: ' + columnOfCellEdited);
var rowOfCellEdited = e.range.getRow();
Logger.log('rowOfCellEdited: ' + rowOfCellEdited);
if (columnOfCellEdited === 3) {
var valueToGet = sh.getRange(rowOfCellEdited, 4);
var dateToGet = "";
if (valueOfCellEdited === "Devis") { //Date to write is already set to an empty string, so no "Else" needed
var dateInColB = sh.getRange(rowOfCellEdited, 2).getValue();
Logger.log('dateInColB: ' + dateInColB);
Logger.log('typeof dateInColB: ' + typeof dateInColB);
dateToGet = new Date(dateInColB);
var dateInMilliseconds = dateToGet.valueOf();
Logger.log('dateInMilliseconds: ' + dateInMilliseconds);
//60 seconds in a minute = 60 * 1000 = 60000 milliseconds in a minute.
//60 minutes in a hour = 60,000 * 60 = 3,600,000
//24 hours in day 3,600,000 * 24 = 86,400,000
var datePlusEight = dateInMilliseconds + (8 * 86400000);
Logger.log('datePlusEight: ' + datePlusEight);
var newDateIs = new Date(datePlusEight);
Logger.log('newDateIs: ' + newDateIs);
Logger.log('newDateIs: ' + newDateIs.toString());
sh.getRange(rowOfCellEdited, 4).setValue(newDateIs);
};
};
};
注释掉Logger.log()
语句以供使用。要进行测试,请在C列中编辑一个单元格,并在其旁边的日期位于同一行,但在B列中。
在电子表格的“工具”菜单下,选择“脚本编辑器”,复制代码并将其粘贴到脚本编辑器中。