【问题标题】:How two combine these two onEdit Script两者如何结合这两个 onEdit 脚本
【发布时间】:2021-08-06 15:40:27
【问题描述】:

在组合这两个脚本方面需要帮助。我在这件事上完全是新手。他们是

Main.gs

当在 U 列(第 21 列)上输入“XX”时,它将整行复制到与 V 列(第 22 列)同名的工作表中。然后删除某些列(例如 6、7、8、12、14、16、17、19、20、21)

function onEdit(e) {

var ss = e.source;
var s = e.range.getSheet();;
var r = e.range;
// to let you modify where the action and move columns are in the form responses sheet
var actionCol = 21;
var nameCol = 22;
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
// -1 to drop our action/status column
var colNumber = s.getLastColumn()-1;
// if our action/status col is changed to ok do stuff
if (e.value == "XX" && colIndex == actionCol) {
// get our target sheet name - in this example we are using the priority column
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
// if the sheet exists do more stuff
if (ss.getSheetByName(targetSheet)) { 
// set our target sheet and target range
var targetSheet = ss.getSheetByName(targetSheet);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
// get our source range/row
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
// new sheets says: 'Cannot cut from form data. Use copy instead.' 
sourceRange.copyTo(targetRange);
// ..but we can still delete the row after
const cols = [6, 7, 8, 12,14,16,17,19,20,21];
for (const col of cols) {
  s.getRange(rowIndex, col).clearContent();
}

// or you might want to keep but note move e.g. r.setValue("moved");
}
}
}

和 时间戳.gs

当在“Pasien”列输入一个单词时,在同一行它会在“Tgl Keluar”列添加日期,当删除该单词时,它也会清除内容

function onEdit(event)
{ 
  var timezone = "GMT+7";
  var time_format = "dd-MM-yyyy"; // Timestamp Format. 
  var updateColName = "Pasien";
  var timeStampColName = "Tgl Keluar";
  var sheet = event.source.getSheetByName('Form'); //Name of the sheet where you want to run this script.

  var actRng = event.source.getActiveRange();
  var editColumn = actRng.getColumn();
  var index = actRng.getRowIndex();
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
  var dateCol = headers[0].indexOf(timeStampColName);
  var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
 
  if (dateCol > -1 && index > 1 && editColumn == updateCol) 
  
  { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
  
  if (sheet.getRange(index, updateCol).isBlank()) {
  sheet.getRange(index, dateCol + 1).clearContent();
}
else 
    var cell = sheet.getRange(index, dateCol + 1);
    var date = Utilities.formatDate(new Date(), timezone, time_format);
    cell.setValue(date);
  }
}

这是我的组合 onEdit 但我仍然不了解嵌套

function onEdit(event) {
  first(event);
  second(event);
}

function first(event) 
{

var ss = e.source;
var s = e.range.getSheet();;
if (s.getName() !== 'Form')
var r = e.range;
// to let you modify where the action and move columns are in the form responses sheet
var actionCol = 21;
var nameCol = 22;
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
// -1 to drop our action/status column
var colNumber = s.getLastColumn()-1;
// if our action/status col is changed to ok do stuff
if (e.value == "XX" && colIndex == actionCol) {
// get our target sheet name - in this example we are using the priority column
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
// if the sheet exists do more stuff
if (ss.getSheetByName(targetSheet)) { 
// set our target sheet and target range
var targetSheet = ss.getSheetByName(targetSheet);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
// get our source range/row
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
// new sheets says: 'Cannot cut from form data. Use copy instead.' 
sourceRange.copyTo(targetRange);
// ..but we can still delete the row after
const cols = [6, 7, 8, 12,14,16,17,19,20,21];
for (const col of cols) {
  s.getRange(rowIndex, col).clearContent();
}

// or you might want to keep but note move e.g. r.setValue("moved");
}
}
}

function second(event)
{ 
  var timezone = "GMT+7";
  var time_format = "dd-MM-yyyy"; // Timestamp Format. 
  var updateColName = "Pasien";
  var timeStampColName = "Tgl Keluar";
  var sheet = event.source.getSheetByName('Form'); //Name of the sheet where you want to run this script.

  var actRng = event.source.getActiveRange();
  var editColumn = actRng.getColumn();
  var index = actRng.getRowIndex();
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
  var dateCol = headers[0].indexOf(timeStampColName);
  var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
 
  if (dateCol > -1 && index > 1 && editColumn == updateCol) 
  
  { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
  
  if (sheet.getRange(index, updateCol).isBlank()) {
  sheet.getRange(index, dateCol + 1).clearContent();
}
else 
    var cell = sheet.getRange(index, dateCol + 1);
    var date = Utilities.formatDate(new Date(), timezone, time_format);
    cell.setValue(date);
  }
}

【问题讨论】:

  • 所有脚本都需要有一个唯一的名字
  • 我不明白事情是如何运作的并且非常需要它们
  • 那我建议雇人帮忙或快速学习。
  • 首先尝试将first(event) 更改为function first(event),并将second(event) 更改为function second(event)。我不知道,但第三个示例有可能会起作用。
  • 问题可能是这两个函数的代码没有在 30 秒内完成,这是一个要求。也许您应该通过学习如何更多地利用事件对象来减少代码的大小。

标签: google-apps-script google-sheets


【解决方案1】:

如果在时限内完成,第三个代码将是最简单的解决方案。

否则,您需要通过以下方式优化代码:

  • SpreadsheetApp 的调用替换为event 对象
  • 删除两个子函数中的重复项
  • 使用ifboolean(标志)、return 最大限度地减少执行次数

你需要知道你在其他方面的代码做得很好才能做到这些。

【讨论】:

  • 这对我来说听起来很模糊,我真的希望我能在这里得到帮助。谢谢你的提示
【解决方案2】:

我不敢完全重写代码。只是为了让它更快一点,我建议改变这行(在第三个例子中):

const cols = [6, 7, 8, 12,14,16,17,19,20,21];
for (const col of cols) {
  s.getRange(rowIndex, col).clearContent();
}

到这里:

// cols to clean
const cols = [6, 7, 8, 12, 14, 16, 17, 19, 20, 21];

// get an array from the row
var row_to_clean = s.getRange(rowIndex, 6, rowIndex, 21).getValues();

// clean the array
for (let c of cols) row_to_clean[0][c-6] = ''; // col 6 is array[0][0]

// set values of the array back on the row
s.getRange(rowIndex, 6, rowIndex, 21).setValues(row_to_clean);

对于每个编辑的行,它将对服务器的调用从 10 个clearContents() 减少到 2 个getValues() + setValues()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2018-10-04
    • 1970-01-01
    相关资源
    最近更新 更多