简体   繁体   中英

How do I push values into a column Google Sheets App Script

I'm searching through the documentation as best I can, I'm just on a time limit here so if someone can tell me that would be great.

I need to insert data into a column and have it push the data in the column down when it's inserted. For example I need to add the word "Good" at the top of the column, "Bad" WAS at the top, but when I pushed in "Good", "Bad" became the number two spot, the number two spot became the number three spot, etc. It needs to do this without deleting or moving the rows themselves because I'm reading data from two columns in the sheet and then writing to a third column.

Thanks in advance!

Welcome to StackOverflow.

From what I understood reading your question is that you have already been able to read data from two column and now you just want to store some of them in a separate column. Apologies if I misunderstood your question.

If I understood your question right, I would suggest you to create a list of the requests and make a batch update of it. Which would help you refraining yourself from reaching the write quota.

So, here how it goes-

request = []
request.append({
      "updateCells": {
           "rows": [
               {
                  "values":[
                     {
                        "userEnteredValue": {
                           "numberValue": 546564      #Assuming your value is integer
                        },
                        "userEnteredFormat": {
                           "horizontalAlignment": "CENTER",
                           "verticalAlignment": "MIDDLE"
                        }
                     }
                  ]
               }
            ],
           "fields": "*",
           "range": {
                #Replace these values with actual values
                "sheetId": sheetId,
                "startRowIndex": startRow,    #Indexing start from 0
                "endRowIndex": endRow,
                "startColumnIndex": startColumn,
                "endColumnIndex": endColumn,
            }
      }
   })
#You can add more requests like this in the list and then execute
body = {
    "requests": request
}
response = sheet.spreadsheets().batchUpdate(
    spreadsheetId=spreadsheet_id,
    body=body).execute()
#If you are using gspread, then you can use this
sheet.batch_update({"requests" : request})

This will update the cells with your given value. For detailed information and other formatting follow the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM