如何下载 R 中 google sheet 中的所有 sheet

How to download all sheets in a google sheet in R

我希望在 R 中的单个 google sheet 中下载所有 sheet。 我目前正在使用 [maxconway][1]gsheet 包,它允许我使用它的 URL 下载 sheet,但它只适用于个人 sheet , 它们由 gid 区分。 我要下载的 google sheet 集有超过 100 个 sheet,这使得用 gsheet 一个一个地下载它们非常不方便 - 有谁知道是否有任何 R 包可以自动执行此操作或以任何方式在单个 google sheet 中循环遍历所有 sheet?
这是我目前拥有的代码,它只下载 100 多个 sheet 中的第一个作为小标题:

all_rolls <- gsheet2tbl('https://docs.google.com/spreadsheets/d/1OEg29XbL_YpO0m5JrLQpOPYTnxVsIg8iP67EYUrtRJg/edit#gid=26346344')

> head(all_rolls)
# A tibble: 6 x 14
  Episode Time   Character `Type of Roll` `Total Value` `Natural Value` `Crit?` `Damage Dealt` `# Kills`
    <int> <drtn> <chr>     <chr>          <chr>         <chr>           <chr>   <chr>              <int>
1       1 37'53" Vex'ahlia Intelligence   20            18              <NA>    <NA>                  NA
2       1 41'48" Grog      Persuasion     19            18              <NA>    <NA>                  NA
3       1 43'25" Keyleth   Persuasion     2             2               <NA>    <NA>                  NA
4       1 46'35" Tiberius  Persuasion     12            3               <NA>    <NA>                  NA
5       1 46'35" Tiberius  Persuasion     27            18              <NA>    <NA>                  NA
6       1 46'35" Percy     Assist         21            15              <NA>    <NA>                  NA
# … with 5 more variables: Notes <chr>, `Non-Roll Kills` <chr>, X12 <chr>, X13 <chr>, X14 <chr>

注意:我尝试删除 #gid 字段,但它只会下载第一个 sheet。

更新 2021-01-31:更新代码以使用从 googlesheets4 版本 0.2 开始替换 sheets_find()sheets_sheets() 的新函数.0.

googlesheets4 软件包包含一个功能,用于列出与帐户的 Google 驱动器关联的所有 sheet:sheets_find()。从 sheet 的列表中,可以使用 sheet ID 将 sheet 读入 R.

library(googlesheets4)
sheets_auth()
theSheets <- gs4_find()
theSheets

我在 Google 上的测试帐户有一个 Google sheet,一个 sheet 的 Pokémon Stats。

> theSheets
# A tibble: 1 x 3
  name         id                                           drive_resource   
* <chr>        <chr>                                        <list>           
1 PokemonStats 13rGxY7ScDUl7bFJ9NipO7QUafEACYTH4MagFjcj4pVw <named list [34]>

我们可以使用ID字段来下载sheet。

pokemonData <- sheets_read(theSheets$id[1])
head(pokemonData)


> head(pokemonData)
# A tibble: 6 x 13
  Number Name  Type1 Type2 Total    HP Attack Defense SpecialAtk SpecialDef Speed
   <dbl> <chr> <chr> <chr> <dbl> <dbl>  <dbl>   <dbl>      <dbl>      <dbl> <dbl>
1      1 Bulb… Grass Pois…   318    45     49      49         65         65    45
2      2 Ivys… Grass Pois…   405    60     62      63         80         80    60
3      3 Venu… Grass Pois…   525    80     82      83        100        100    80
4      3 Venu… Grass Pois…   625    80    100     123        122        120    80
5      4 Char… Fire  NA      309    39     52      43         60         50    65
6      5 Char… Fire  NA      405    58     64      58         80         65    80
# … with 2 more variables: Generation <dbl>, Legendary <lgl>
> 

可以使用向量 theSheets$idlapply() 从 Google 驱动器中读取一组 sheets,如下所示:

sheetList <- lapply(theSheets$id,sheet_read)

要在 Google 张传播sheet 中阅读多个作品sheet,我们将 sheet= 参数添加到 sheet_read()。在这里,我们阅读了 Pokémon Stats spreadsheet.

中第二部作品sheet 的 Pokémon 类型
pokemonTypes <- sheets_read(theSheets$id[1],sheet = 2)
head(pokemonTypes)

...输出:

> head(pokemonTypes)
# A tibble: 6 x 1
  Type    
  <chr>   
1 Fire    
2 Grass   
3 Poison  
4 Water   
5 Bug     
6 Fighting
> 

正在阅读所有作品sheet 展开sheet

我们可以自动化从单个跨页读取多个标签的过程sheet。 sheets_sheets() 函数可用于此目的。

# technique where we read multiple worksheets by worksheet name
# using functions from googlesheets4 version 0.2.0. 
theSheets <-gs4_find()
# get metadata from first sheet
sheetMetadata <- gs4_get(theSheets$id[1])
# get worksheet tab names
sheetNames <- sheet_names(theSheets$id[1])
sheetNames

此时我们可以看到 Pokémon Stats spreadsheet 中有两个工作sheet 选项卡。我们使用向量 sheetNameslapply() 来读取主传播sheet.

中的所有作品sheet
theWorksheets <- lapply(sheetNames, function(x){             
     sheets_read(theSheets$id[1],sheet = x) 
})
# use the `names()` function to name the data frames stored in the list
names(theWorksheets) <- sheetNames
lapply(theWorksheets,head)

...输出:

> lapply(theWorksheets,head)
$Pokemon
# A tibble: 6 x 13
  Number Name  Type1 Type2 Total    HP Attack Defense SpecialAtk SpecialDef Speed
   <dbl> <chr> <chr> <chr> <dbl> <dbl>  <dbl>   <dbl>      <dbl>      <dbl> <dbl>
1      1 Bulb… Grass Pois…   318    45     49      49         65         65    45
2      2 Ivys… Grass Pois…   405    60     62      63         80         80    60
3      3 Venu… Grass Pois…   525    80     82      83        100        100    80
4      3 Venu… Grass Pois…   625    80    100     123        122        120    80
5      4 Char… Fire  NA      309    39     52      43         60         50    65
6      5 Char… Fire  NA      405    58     64      58         80         65    80
# … with 2 more variables: Generation <dbl>, Legendary <lgl>

$Metadata
# A tibble: 6 x 1
  Type    
  <chr>   
1 Fire    
2 Grass   
3 Poison  
4 Water   
5 Bug     
6 Fighting

> 

此时可以使用提取运算符的 $ 形式访问个人作品sheet,如 theWorksheets$PokemontheWorksheets$Metadata.