Gen*_*had 9 google-sheets google-apps-script
我需要检测是否在某个数据范围内对电子表格进行了更改,如果是,请设置当前更新时间.
问题是,我有一个电子表格,我编辑标题和文本,我不希望在电子表格上更新特定单元格中的更新时间,但当我编辑一系列单元格中的数据时,我想要更新时间变了.
这是我必须更新的时间.
function onEdit(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange("G10").setValue(new Date());
} ?
Run Code Online (Sandbox Code Playgroud)
如果我编辑某些单元格(在"B4:J6"范围内),我只想在G10中设置日期
Mog*_*dad 21
有一个事件作为参数提供给您的onEdit()函数,它包含有关编辑内容的必要信息.如果你想知道那(e)是什么,就是这样.
由于每次编辑onEdit()都会调用一个函数,因此您应该尽可能少地进行处理以确定是否应该退出.通过使用传入的事件,您将需要更少的服务调用,因此将更有效.如果您需要灵活,Rasmus回答将A1表示法转换为列和行号的方式很好,但如果编辑范围是固定的,您可以简单地使用常数值进行比较 - 再次,减少所需的处理时间.
function onEdit(e)
{
var editRange = { // B4:J6
top : 4,
bottom : 6,
left : 2,
right : 10
};
// Exit if we're out of range
var thisRow = e.range.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
var thisCol = e.range.getColumn();
if (thisCol < editRange.left || thisCol > editRange.right) return;
// We're in range; timestamp the edit
var ss = e.range.getSheet();
ss.getRange(thisRow,7) // "G" is column 7
.setValue(new Date()); // Set time of edit in "G"
} ?
Run Code Online (Sandbox Code Playgroud)
通过命名“触发”范围,一旦在脚本中设置了范围名称,就不必弄乱脚本。通过简单地在标准google表格界面内编辑您命名范围的范围,即使您扩大或缩小范围的大小,该脚本仍然可以使用。
如果您按照此处的说明编辑范围内的任何单元格,请命名您希望用作脚本触发器的单元格范围:https : //support.google.com/docs/answer/63175
我将范围命名为“ triggerRange”。
function onEdit(e) {
var myRange = SpreadsheetApp.getActiveSheet().getRange('triggerRange'); //<<< Change Your Named Ranged Name Here inside the getRange() function.
//SpreadsheetApp.getUi().alert("myRange in A1 Notation is: " + myRange.getA1Notation()); //If you're having problems, uncomment this to make sure your named range is properly defined
//Let's get the row & column indexes of the active cell
var row = e.range.getRow();
var col = e.range.getColumn();
//SpreadsheetApp.getUi().alert('The Active Cell Row is ' + row + ' and the Column is ' + col); //uncomment this out to do testing
//Check that your active cell is within your named range
if (col >= myRange.getColumn() && col <= myRange.getLastColumn() && row >= myRange.getRow() && row <= myRange.getLastRow()) { //As defined by your Named Range
SpreadsheetApp.getUi().alert('You Edited a Cell INSIDE the Range!');//Repalace Your Custom Code Here
} else {
SpreadsheetApp.getUi().alert('You Edited a Cell OUTSIDE the Range!');//Comment this out or insert code if you want to do something if the edited cells AREN'T inside your named range
return;
}
}
Run Code Online (Sandbox Code Playgroud)
PS:感谢其他张贴者为该基本脚本提供了基本框架。我显然已经对该脚本进行了很多评论,因此您可以轻松对其进行测试。删除我在脚本内部创建的警报,以使外观更整洁……或仅复制并粘贴以下内容:
function onEdit(e) {
var myRange = SpreadsheetApp.getActiveSheet().getRange('triggerRange'); //<<< Change Your Named Ranged Name Here
//Let's get the row & column indexes of the active cell
var row = e.range.getRow();
var col = e.range.getColumn();
//Check that your active cell is within your named range
if (col >= myRange.getColumn() && col <= myRange.getLastColumn() && row >= myRange.getRow() && row <= myRange.getLastRow()) { //As defined by your Named Range
SpreadsheetApp.getUi().alert('You Edited a Cell INSIDE the Range!');//Repalace Your Custom Code Here
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29501 次 |
| 最近记录: |