【问题标题】:Google sheets script to add new rows with value by defaultGoogle 表格脚本默认添加具有值的新行
【发布时间】:2022-10-17 09:38:00
【问题描述】:

在工作表中,我使用一个脚本来删除空行并在底部添加新行。一切正常,但我想改变一点。

添加的新行的所有列不包含公式,除了 L 和 U 列。如果我可以在这些新添加的行的 L 和 U 列中有“NO”值,则不需要这些公式。

因此,我希望不要使用公式添加新行,而是在 L 和 U 列中添加值“NO”。

https://docs.google.com/spreadsheets/d/1CPcMx3Dhbqi-zO4D3jYNxO-PGjyW3iTfRo5gRmEB9p4/edit#gid=0

function removeEmpty() {
  const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients');
  const empty_rows = [];
  const lastRow = sh.getLastRow()
  const data = sh.getRange("C6:G" + lastRow).getValues();
  for (var i in data) if (data[i].join('') == '') empty_rows.push(+i + 6);
  empty_rows.reverse().forEach(x => sh.deleteRow(x));
  sh.insertRowsAfter(lastRow - empty_rows.length, 5)

  var rng = sh.getRange('A7:Z7')
  rng.copyTo(sh.getRange('A' + (lastRow - empty_rows.length + 1) + ':Z' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);

  // L
  var rng = sh.getRange('L' + (lastRow - empty_rows.length) + ':L' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('L' + (lastRow - empty_rows.length + 1) + ':L' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
  // U
  var rng = sh.getRange('U' + (lastRow - empty_rows.length) + ':U' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('U' + (lastRow - empty_rows.length + 1) + ':U' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);

}

【问题讨论】:

  • 我找到了一个解决方案,但我将默认值放在所有列中,但我找不到如何将其仅放在新添加的行中。

标签: google-apps-script google-sheets row


【解决方案1】:

终于自己找到了

function removeEmpty() {
  const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients');
  const empty_rows = [];
  const lastRow = sh.getLastRow()
  const data = sh.getRange("C6:G" + lastRow).getValues();
  for (var i in data) if (data[i].join('') == '') empty_rows.push(+i + 6);
  empty_rows.reverse().forEach(x => sh.deleteRow(x));
  sh.insertRowsAfter(lastRow - empty_rows.length, 5)

  var rng = sh.getRange('A7:Z7')
  rng.copyTo(sh.getRange('A' + (lastRow - empty_rows.length + 1) + ':Z' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);

  // L
  var rng = sh.getRange('L' + (lastRow - empty_rows.length) + ':L' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('L' + (lastRow - empty_rows.length + 1) + ':L' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients').getRange("L6:L").setValue('NON'));
  // U
  var rng = sh.getRange('U' + (lastRow - empty_rows.length) + ':U' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('U' + (lastRow - empty_rows.length + 1) + ':U' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients').getRange("U6:U").setValue('NON'));

}

【讨论】:

  • 在这个答案中,我将默认值放在所有列中,但我找不到如何将其仅放在新添加的行中。
  • 还没找到解决办法。。
猜你喜欢
  • 2013-12-18
  • 1970-01-01
  • 2018-05-01
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多