How to Autofill last row with formula, when data is received from IFTTT on the last row?

前端 未结 2 561
庸人自扰
庸人自扰 2021-01-17 08:02

I have a spreadsheet: https://docs.google.com/spreadsheets/d/1df2cp4DsJvSeBvhsNjLgIa5x_RO1X7s_APRdFzU6jqQ/edit?usp=sharing

|    | C                                    


        
相关标签:
2条回答
  • 2021-01-17 09:00

    Solution:

    • This solution uses google-apps-script. This does not need array formulas, but uses autofill. For the solution to work, you should not use array formula, but plain formula.
    • Get the active range, which will represent the current range inserted onChange or onFormSubmit.
    • offset the range to the right to get the calculated columns and expand the range to all of calculated columns on the right.
    • Use range.autoFillToNeighbor to fill all the calculated regions of the range.

    Sample script:

    /**
     * @param {GoogleAppsScript.Events.SheetsOnChange|GoogleAppsScript.Events.SheetsOnFormSubmit} e
     */
    const onFormSubmitORonChange = e => {
      const rg = SpreadsheetApp.getActiveRange(),
        wd = rg.getWidth(),
        sh = rg.getSheet(),
        lc = sh.getLastColumn(),
        numRows = Math.max(
          rg
            .offset(0, wd, 1, 1)
            .getNextDataCell(SpreadsheetApp.Direction.UP)
            .getRow() - 1,
          1
        ),
        numCols = lc - wd;
      sh.getRange(2, wd + 1, numRows, numCols).autoFillToNeighbor(
        SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES
      );
    };
    

    References:

    • How to create a onFormSubmit or onChange trigger for this function?
    0 讨论(0)
  • 2021-01-17 09:08

    This is a very common problem when dealing with auto-inserted data through sheets api or Google forms. The easiest solution would be to convert all your formulas into arrayformulas. For eg, Your formula in D2

    =MID(C2,1,2)&"."&MID(C2,3,2)&".2020
    

    can be modified as

    In D1:

    =ARRAYFORMULA({"Extracted Date";MID(C2:INDEX(C:C,COUNTA(C:C)),1,2)&"."&MID(C2:INDEX(C:C,COUNTA(C:C)),3,2)&".2020"})
    

    OR using regex:

    =ARRAYFORMULA({"Extracted Date";REGEXREPLACE(C2:INDEX(C:C,COUNTA(C:C)),"(\d{2})(\d{2}).*","$1.$2.2020")})
    

    The table then becomes:

    |    | C                               | D                                                                                                                   |
    |----+---------------------------------+---------------------------------------------------------------------------------------------------------------------|
    | 1> | From IFTTT                      | =ARRAYFORMULA({"Extracted Date";MID(C2:INDEX(C:C,COUNTA(C:C)),1,2)&"."&MID(C2:INDEX(C:C,COUNTA(C:C)),3,2)&".2020"}) |
    | 2> | 0809 1800 0909 0600 RLK Steiger |                                                                                                                     |
    | 3> | 0809 1800 0909 0600 RLK Dvorak  |                                                                                                                     |
    | 4> | 0909 0600 0909 1800 UNIS Brando |                                                                                                                     |
    

    Here the rest of D:D is auto filled automatically.

    • We use array literals on the header row: {"Extracted Date";Formula for rest of the column}

    • Whenever a new row comes, INDEX/COUNTA() auto calculates the last row and automatically fills the formula upto last row. See here for a deeper explanation on INDEX/COUNTA.

    0 讨论(0)
提交回复
热议问题