编辑范围内的任何单元格(C1-C50)时,在特定单元格(C1)中的Google表/AppScript/显示日期进行编辑
JS的新手(慢慢地通过我的方式),但是我在Google表上遇到了一个问题,我想在特定单元格中显示一个日期(C1),当任何单元格在一个范围内编辑时(C2) :C50)。
理想情况下,我想将其应用于多个范围(5-c2:50),但要单独处理每个范围,以便在每个单独范围内进行编辑时,它将填充相应的范围第一行(C2:C50> PUPERTULE C1等)。
仅接受第一个编辑而不会更改C1:G1中的值,这很方便,但是我不确定以这种方式实施它是一个好主意。
我尝试修改以下脚本,但找不到选择特定单元格的正确函数,而不是从该范围内单个单元格中的偏移。
事先感谢任何可以提供帮助的人!
/**
* Creates a Date Stamp if a column is edited.
*/
//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK = 1;
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,1];
// Sheet you are working on
var SHEETNAME = 'Sheet1'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//checks that we're on the correct sheet.
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
//checks the column to ensure it is on the one we want to cause the date to appear.
if( selectedCell.getColumn() == COLUMNTOCHECK) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}
New to JS (Slowly working my way through), but I've got an issue on a Google Sheet where I'd like to have a date displayed in a specific Cell (C1), when any cell is edited in a range (C2:C50).
Ideally I'm wanting to apply this to multiple ranges (5 - C2:50) but to treat each range individually so that when an edit occurs within each individual range, it will populate the corresponding ranges first row (C2:C50 > Populate C1 etc).
It would be convenient to have it only accept the first edit and not change the value in C1:G1 on further edits, but I'm not sure it's a good idea to implement it this way.
I've tried modifying the below script, but couldn't find the right function to select a specific cell rather than an offset from an individual cell in the range.
Thanks in advance to anyone that can help!
/**
* Creates a Date Stamp if a column is edited.
*/
//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK = 1;
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,1];
// Sheet you are working on
var SHEETNAME = 'Sheet1'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//checks that we're on the correct sheet.
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
//checks the column to ensure it is on the one we want to cause the date to appear.
if( selectedCell.getColumn() == COLUMNTOCHECK) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您的目标如下。
new Date()
的值放在单元格“ C1”中。oneDit
简单触发器。在这种情况下,以下修改后的脚本怎么样?
修改后的脚本:
请将以下脚本复制到电子表格的脚本编辑器并保存脚本。使用此脚本时,请编辑“ Sheet1”的“ C2:C50”单元格。这样,该脚本由
Onedit
的简单触发器自动运行。这样,new Date()
将其放在单元格“ C1”上。注意:
ONEDIT
触发器运行。因此,当您直接使用脚本编辑器直接运行此脚本时,会出现typeerror:无法读取未定义
的属性的“范围”的错误。请注意。使用此脚本时,请编辑“ Sheet1”的“ C2:C50”单元格。参考:
/
event
示例脚本如下。
示例脚本:
请将要使用的列号设置为
var ColumnToCheck = [3,4,5];
。new Date()
的值放在每列的第一行中。如果您始终想将其放在单元格“ C1”中,请修改getrange(1,col)
getrange(“ c1”)
。I believe your goal is as follows.
new Date()
to the cell "C1".onEdit
simple trigger.In this case, how about the following modified script?
Modified script:
Please copy and paste the following script to the script editor of Spreadsheet and save the script. When you use this script, please edit the cells "C2:C50" of "Sheet1". By this, the script is automatically run by the simple trigger of
onEdit
. By this,new Date()
is put to the cell "C1".Note:
onEdit
trigger. So when you directly run this script with the script editor, an error likeTypeError: Cannot read property 'range' of undefined
occurs. Please be careful about this. When you use this script, please edit the cells "C2:C50" of "Sheet1".References:
Added:
About the following new question,
The sample script is as follows.
Sample script:
Please set the column numbers you want to use to
var COLUMNTOCHECK = [3, 4, 5];
.new Date()
is put to the 1st row of each column. If you always want to put it to the cell "C1", please modifygetRange(1, col)
togetRange("C1")
.