在 google 电子表格中插入列时使用批处理请求

Using batch request when insert columns in google spreadsheet

我正在尝试将大量列插入 table,我需要插入 2 列,现在我的代码如下所示

sheet.insertColumnAfter(16),sheet.insertColumnAfter(18),sheet.insertColumnAfter(20),sheet.insertColumnAfter(22)

我试过用数组,没用,不胜感激

我相信你的目标如下。

  • 您想使用 Google Apps 脚本插入 insertColumnAfter(16)insertColumnAfter(18)insertColumnAfter(20)insertColumnAfter(22) 等列。
  • 当您的目标实现时,插入的列是列“Q”、“T”、“W”和“Z”。
  • 您想使用数组实现此目的。

修改点:

  • 在这种情况下,如何插入从 insertColumnAfter(22)insertColumnAfter(16) 的列?
    • 我认为这可能是您遇到问题的原因。
  • 为了使用数组,准备一个包含列号的数组,并使用数组插入列。

以上几点反映到脚本中,就变成了下面的样子。

修改后的脚本:

const sheet = SpreadsheetApp.getActiveSheet();
const columns = [16, 18, 20, 22];
columns.reverse().forEach(c => sheet.insertColumnAfter(c));
  • 在此脚本中,使用了活动 sheet。所以请根据自己的实际情况修改。
  • 当此脚本为运行时,从第22列插入到第16列。这样,插入的列为“Q”、“T”、“W”和“ Z".

参考文献: