简体   繁体   English

带有选定列脚本的 Google 表格单独的日志页面

[英]Google sheet separate log page with sellected columns script

I have a Google Sheet problem.我有一个谷歌表格问题。 My main page (which name is "MAIN", collects its data from other sheets. So, 'MAIN' sheet looks like this: MAIN我的主页(名称为“MAIN”,从其他工作表中收集数据。因此,“MAIN”工作表如下所示: MAIN

I want to create a separate log page, which collects only "MAIN" sheets data when ever somebody change his hotel, next to his name.我想创建一个单独的日志页面,当有人更改他的酒店时,它只收集“主要”工作表数据,在他的名字旁边。 (Hotel column is 4th on the MAIN sheet) (酒店栏在 MAIN 表的第 4 位)

..So "LogSheet" needs to collect 1st second and 4th column with timestamp.. ..所以“LogSheet”需要收集带有时间戳的第 1 秒和第 4 列。

so at the end there will be a list (who went, which hotell and when).所以最后会有一个列表(谁去了,哪家酒店和什么时候)。 it needs to be like this, LogSheet but I couldnt write it:/它需要是这样的, LogSheet ,但我写不出来:/

function onEdit(e) {
  
  var sheetsToWatch = ["MAIN"];
  var changelogSheetName = ["LogSheet"];

  var timestamp = new Date();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  var sheetName = sheet.getName();
  
  if(col === 4 && row > 1 && e.source.sheetsToWatch() .getName() === "MAIN" ){
  e.changelogSheetName().getRange('MAIN', row,1).setValue(new Date());
  }   

 }

There is some confusion about the usage of event objects and variables关于事件对象和变量的使用存在一些混淆

  1. Variables (including arrays) are called without () - () are reserved for calls of functions变量(包括数组)在没有()的情况下被调用 - ()保留用于函数调用
  2. If your variable cntains only one value, not an array - there is no need to put it in [] brackets.如果您的变量仅包含一个值,而不是数组 - 则无需将其放在[]括号中。 [] brackets designate a variable as an array and it needs to be proceeded differently. []括号将变量指定为数组,并且需要以不同的方式处理。
  3. To verify either a sheet is contained in an array of sheetnames, you need to use eg indexOf() instead of ==要验证工作表是否包含在工作表名称数组中,您需要使用例如indexOf()而不是==
  4. e.source returns you the spreadsheet in which the edit has taken place. e.source返回进行编辑的电子表格。 When querying for e.source.getActiveSheet() you will obtain in most situations correctly the sheet in which the edit took place, but to account for exceptions it is better to use the event object range together with the method https://developers.google.com/apps-script/reference/spreadsheet/range#getsheet在查询e.source.getActiveSheet()时,您将在大多数情况下正确获得进行编辑的工作表,但考虑到例外情况,最好将事件 object range与方法https://developers 一起使用。 google.com/apps-script/reference/spreadsheet/range#getsheet
  5. The method getRange() expects rows, columns and optionally row and column numbers - not sheet names as variables.方法getRange()需要行、列以及可选的行和列号 - 而不是工作表名称作为变量。

Please have a look at the modifications in the following samples.请查看以下示例中的修改。

Sample with a sheet array带有工作表数组的示例

function onEdit(e) {
  
  var sheetsToWatch = ["MAIN", "add another sheet if desired" ];
  var changelogSheetName = "LogSheet";
  var logSheet = SpreadsheetApp.getActive().getSheetByName(changelogSheetName);
  var timestamp = new Date();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  var sheet = e.range.getSheet();
  var sheetName = sheet.getName();
  Logger.log(e.range.getSheet().getName());
  if(col === 4 && row > 1 && sheetsToWatch.indexOf(sheetName)!=-1){
    logSheet.getRange(row,1).setValue(new Date());
    //do whatever else you want
  }     
}

Sample with a single sheet as a variable以单张纸作为变量的样本

function onEdit(e) {
  
  var sheetsToWatch = "MAIN";
  var changelogSheetName = "LogSheet";
  var logSheet = SpreadsheetApp.getActive().getSheetByName(changelogSheetName);
  var timestamp = new Date();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  var sheet = e.range.getSheet();
  var sheetName = sheet.getName();
  Logger.log(e.range.getSheet().getName());
  if(col === 4 && row > 1 && sheetsToWatch ==sheetName){
    logSheet.getRange(row,1).setValue(new Date());
    //do whatever else you want
  }     
}

Please refer to Apps Script documentation and Guides for better understanding.请参阅 Apps 脚本文档指南以获得更好的理解。

UPDATE更新

For your case, the most convenient is to retrieve values from the edited row and append them in the desired order to LogSheet对于您的情况,最方便的是从已编辑的行中检索值,并按所需的顺序将 append 检索到LogSheet

Sample样本

function onEdit(e) {
  
  var sheetsToWatch = "MAIN";
  var changelogSheetName = "LogSheet";
  var logSheet = SpreadsheetApp.getActive().getSheetByName(changelogSheetName);
  var timestamp = new Date();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  var sheet = e.range.getSheet();
  var sheetName = sheet.getName();
  if(col === 4 && row > 1 && sheetsToWatch ==sheetName){
    var values = sheet.getRange(row,1,1,4).getValues()[0];
    logSheet.appendRow([new Date(), values[0], values[1], values[3]]);
  }     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM