google-apps-scriptbatch-request

Using batch request when insert columns in google spreadsheet


I'm trying to insert a large number of columns into a table, I need to insert through 2 columns, now my code looks like this

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

I tried to use an array, nothing worked, I will be very grateful


Solution

  • I believe your goal as follows.

    Modification points:

    When above points are reflected to the script, it becomes as follows.

    Modified script:

    const sheet = SpreadsheetApp.getActiveSheet();
    const columns = [16, 18, 20, 22];
    columns.reverse().forEach(c => sheet.insertColumnAfter(c));
    

    References: