Google Apps脚本将公式替换为当前行中的文本

时间:2018-11-08 19:11:18

标签: google-apps-script

我正在尝试在Google表格中编写脚本,以便如果我更改F列中的单元格,它将在b:e列的同一行上复制并粘贴值。我将不胜感激。

我有一个Google表格,操作员可以在其中记录事件。他们在a列中输入参考编号,并从另一个选项卡中查找相应的数据,并将其显示在b-e列中。然后,他们在F-M列中添加数据,例如其名称,当前时间等。我正在尝试编写一个脚本,以便当我更改F列时,它将复制当前行中b-e列中公式的结果并将其粘贴回值中。

原因有两点,一是删除公式并粘贴值可以提高性能,二是如果有人更改了查询数据,则该行将损坏。

我发现以下插入时间戳的脚本-似乎可以进行较小的修改,但我无法弄清楚。 (感谢本文的作者)

//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,11];
// Sheet you are working on
var SHEETNAME = 'ReceivingLog'

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());
      }
  }
}

1 个答案:

答案 0 :(得分:0)

部分解决:第二个函数(下面)作为一个独立的副本粘贴值,但它不喜欢将两个函数像这样串在一起。寻找有关如何使两个脚本一起运行的帮助。

//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,11];
// Sheet you are working on
var SHEETNAME = 'ReceivingLog'

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());
      }
  }
}

//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK2 = 2;
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION2 = [0,-1];

// Sheet you are working on
var SHEETNAME2 = 'PurchaseOrders'

function onEdit2(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  //checks that we're on the correct sheet.
  if( sheet.getSheetName() == SHEETNAME2 ) { 
    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() == COLUMNTOCHECK2) { 
      var dateTimeCell = selectedCell.offset(DATETIMELOCATION2[0],DATETIMELOCATION2[1]);
      var data = dateTimeCell.getValues()
      dateTimeCell.setValue(data) , {contentsOnly: true};
      }
  }
}