當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R googlesheets4 sheet_append 將行附加到工作表


在(工作)表中包含數據的最後一行之後添加一個或多個新行,並根據需要增加工作表的行尺寸。

用法

sheet_append(ss, data, sheet = 1)

參數

ss

識別 Google 表格的內容:

  • 其文件 ID 作為字符串或 drive_id

  • 我們可以從中恢複 id 的 URL

  • one-row dribble ,這就是 googledrive 表示雲端硬盤文件的方式

  • googlesheets4_spreadsheet 的實例,這就是 gs4_get() 返回的內容

通過 as_sheets_id() 處理。

data

一個 DataFrame 。

sheet

要附加到的工作表,即 "worksheet" 或 "tab"。您可以通過名稱(使用字符串)或位置(使用數字)來標識工作表。

輸入 ss ,作為 sheets_id 的實例

例子

# we will recreate the table of "other" deaths from this example Sheet
(deaths <- gs4_example("deaths") %>%
  range_read(range = "other_data", col_types = "????DD"))
#> ✔ Reading from deaths.
#> ✔ Range other_data.
#> # A tibble: 10 × 6
#>    Name        Profession   Age `Has kids` `Date of birth` `Date of death`
#>    <chr>       <chr>      <dbl> <lgl>      <date>          <date>         
#>  1 Vera Rubin  scientist     88 TRUE       1928-07-23      2016-12-25     
#>  2 Mohamed Ali athlete       74 TRUE       1942-01-17      2016-06-03     
#>  3 Morley Saf… journalist    84 TRUE       1931-11-08      2016-05-19     
#>  4 Fidel Cast… politician    90 TRUE       1926-08-13      2016-11-25     
#>  5 Antonin Sc… lawyer        79 TRUE       1936-03-11      2016-02-13     
#>  6 Jo Cox      politician    41 TRUE       1974-06-22      2016-06-16     
#>  7 Janet Reno  lawyer        78 FALSE      1938-07-21      2016-11-07     
#>  8 Gwen Ifill  journalist    61 FALSE      1955-09-29      2016-11-14     
#>  9 John Glenn  astronaut     95 TRUE       1921-07-28      2016-12-08     
#> 10 Pat Summit  coach         64 TRUE       1952-06-14      2016-06-28     

# split the data into 3 pieces, which we will send separately
deaths_one <- deaths[1:5, ]
deaths_two <- deaths[6, ]
deaths_three <- deaths[7:10, ]

# create a Sheet and send the first chunk of data
ss <- gs4_create("sheet-append-demo", sheets = list(deaths = deaths_one))
#> ✔ Creating new Sheet: sheet-append-demo.

# append a single row
ss %>% sheet_append(deaths_two)
#> ✔ Writing to sheet-append-demo.
#> ✔ Appending 1 row to deaths.

# append remaining rows
ss %>% sheet_append(deaths_three)
#> ✔ Writing to sheet-append-demo.
#> ✔ Appending 4 rows to deaths.

# read and check against the original
deaths_replica <- range_read(ss, col_types = "????DD")
#> ✔ Reading from sheet-append-demo.
#> ✔ Range deaths.
identical(deaths, deaths_replica)
#> [1] TRUE

# clean up
gs4_find("sheet-append-demo") %>%
  googledrive::drive_trash()
#> File trashed:
#> • sheet-append-demo <id: 13v8BAdPvOB6Dd9HV3UaLWmhDeS1iDK-Ky-2HJGhMnpg>
源代碼:R/sheet_append.R

相關用法


注:本文由純淨天空篩選整理自Jennifer Bryan等大神的英文原創作品 Append rows to a sheet。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。