更正时间戳脚本..快速问 [英] Correcting timestamp script .. Quick Q

查看:91
本文介绍了更正时间戳脚本..快速问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对脚本非常陌生,所以我只是想让这个脚本适用于我的工作表。

  function onEdit(){
var s = SpreadsheetApp.getActiveSheet();
if(s.getName()==Sheet1){//检查我们是否在正确的表单上
var r = s.getActiveCell();
if(r.getColumn()== 5){//检查列
var nextCell = r.offset(0,1);
if(nextCell.getValue()==='')//是空的?
nextCell.setValue(new Date());
}
}
}

目前看来它如果数据插入到E中,将在列F中输入时间戳。
我希望时间戳记到A中;而不是nextCell,对应A列的是什么?



感谢并且为新手问题感到抱歉!

更新



谢谢!如果我希望这适用于多张表,但不是全部。



我尝试添加额外的行,但它似乎没有工作..我哪里出错了?

 函数onEdit(){
var s = SpreadsheetApp.getActiveSheet();
if(s.getName()==Sanshiro){//检查我们是否在正确的表单上
var r = s.getActiveCell();
if(r.getColumn()== 5){//检查列
var nextCell = r.offset(0,-4);
if(nextCell.getValue()==='')//是空的?
nextCell.setValue(new Date());

if(s.getName()==Josh){//检查我们是否在正确的表单上
var r = s.getActiveCell();
if(r.getColumn()== 5){//检查列
var nextCell = r.offset(0,-4);
if(nextCell.getValue()==='')//是空的?
nextCell.setValue(new Date());

if(s.getName()==Suil){//检查我们是否位于正确的表单上
var r = s.getActiveCell();
if(r.getColumn()== 5){//检查列
var nextCell = r.offset(0,-4);
if(nextCell.getValue()==='')//是空的?
nextCell.setValue(new Date());




解决方案

您的代码使用抵消方法来更新单元格:


偏移量(rowOffset,columnOffset)



返回一个新的范围,这个范围由给定数量的
的行和列(可以是负数)从这个范围中偏移。新的范围将是与原始范围相同的


所以,如果你想写4列到(A列),而不是右边的一列(F列),只需替换这一行:

  var nextCell = r .offset(0,1); 

附带:

  var nextCell = r.offset(0,-4); 


I'm very new to scripts so I'm just trying to get this script working for my sheet.

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 5 ) { //checks the column
      var nextCell = r.offset(0, 1);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setValue(new Date());
    }
  }
}

At the moment it seems it will enter a timestamp in column F if data is inserted into E. I'd like the time stamp to go into A; Instead of nextCell, what would correspond to column A?

Thanks and sorry for the newbie question!

Updated

Thanks! That worked great and helped with my understanding too : )

If I want this to apply to multiple sheets but not all.

I tried adding additional lines but it doesn't seem to have worked.. where have I gone wrong?

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Sanshiro" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 5 ) { //checks the column
      var nextCell = r.offset(0, -4);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setValue(new Date());
    }
    if( s.getName() == "Josh" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 5 ) { //checks the column
      var nextCell = r.offset(0, -4);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setValue(new Date());
    }
      if( s.getName() == "Suil" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 5 ) { //checks the column
      var nextCell = r.offset(0, -4);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setValue(new Date());
    }
  }
}

解决方案

Your code uses the offset method to get the cell to be updated:

offset(rowOffset, columnOffset)

Returns a new range that is offset from this range by the given number of rows and columns (which can be negative). The new range will be the same size as the original range.

So, if you want to write 4 columns to the left (column A) instead of one to the right (column F), just replace this line:

var nextCell = r.offset(0, 1);

with:

var nextCell = r.offset(0, -4);

这篇关于更正时间戳脚本..快速问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆