R语言 提取Excel工作表选项卡颜色

kxeu7u2r  于 2023-10-13  发布在  其他
关注(0)|答案(2)|浏览(121)

我们有多个Excel文件,每个都有多个工作表(选项卡),我们希望从彩色工作表中提取表格,例如“绿色”。
绿色工作表的命名方式不同,因此我们无法根据名称进行选择,并且绿色工作表的顺序在文件之间有所不同,因此我们无法根据顺序进行选择。
我们如何识别绿色单订单?

OP来自Twitter:

https://twitter.com/FilmicAesthetic/status/1707006813122572595
为感兴趣的人概述问题-我很乐意听到使用任何方法的解决方案!
您有:

  • 30 .xlsx文件
  • 每个文件20个字节(有些颜色为绿色,名称不唯一)

我们希望:

  • 仅在绿色选项卡中合并合并表
lx0bsm1f

lx0bsm1f1#

使用 openxlsx2 包导入,提取sheetPr,然后使用 rvest(或使用正则表达式)提取颜色:

library(openxlsx2)
library(rvest)

wb <- wb_load("file1.xlsx")

# extract sheetPr
x <- sapply(wb$worksheets, function(i) i$sheetPr)
x
# [1] "<sheetPr><tabColor rgb=\"FFFF0000\"/></sheetPr>"
# [2] "<sheetPr><tabColor rgb=\"FF92D050\"/></sheetPr>"

#it is an html string, read it and extract the colours
sheetCols <- sapply(wb$worksheets, function(i)
  substring(html_attr(html_node(read_html(i$sheetPr), "tabcolor"), "rgb"), 3))

sheetCols
# [1] "FF0000" "92D050"

which(sheetCols == "92D050")
# [1] 2
v09wglhw

v09wglhw2#

如果OP认为选项卡是一个选项,那么很容易用选项卡检查选项卡的颜色。

Sub demo()
    Dim wk As Workbook
    Dim sht As Worksheet
    Set wk = Workbooks.Open("file1.xlsx")
    For Each sht In wk.Sheets
        If sht.Tab.Color = &H92D050 Then  '&H92D050 = 9621584
            MsgBox sht.Name
            ' your code to consolidate the data
        End If
    Next
    wk.Close False
End Sub

更新

问题:您可以通过excel文件循环和出口只有绿色表从这些文件到一个新的文件与一个单一的工作表?
搜索所有文件并将绿色选项卡合并到工作簿的示例代码。

Sub MergeGreenSheets()
    Dim FolderPath As String, FileName As String
    Dim wk As Workbook, MergeWk As Workbook
    Dim sht As Worksheet, MergeSht As Worksheet
    Dim LastRow As Long
    ' Set the target folder path
    FolderPath = "D:\Temp\Data\"
    ' Create a new workbook for merging data
    Set MergeWk = Workbooks.Add
    Set MergeSht = MergeWk.ActiveSheet
    ' Loop through all the xlsx files in the target folder
    FileName = Dir(FolderPath & "*.xlsx")
    Application.ScreenUpdating = False
    Do While FileName <> ""
        Set wk = Workbooks.Open(FolderPath & FileName)
        ' Loop through all the worksheets in the current workbook
        For Each sht In wk.Sheets
            ' Check if the worksheet tab color is green
            If sht.Tab.Color = &H92D050 Then
                arrData = sht.UsedRange.Value
                MergeSht.Cells(LastRow + 1, 1).Resize(UBound(arrData), UBound(arrData, 2)).Value = arrData
                ' Copy data (with cell format) to the merging worksheet
                ' sht.UsedRange.Copy MergeSht.Cells(LastRow + 1, 1)
                ' Application.CutCopyMode = False
                LastRow = MergeSht.Cells(MergeSht.Rows.Count, 1).End(xlUp).Row
            End If
        Next sht
        ' Close the current workbook without saving changes
        wk.Close False
        FileName = Dir
    Loop
    ' Save the merged data to MergeData.xlsx in the D:\Data folder
    MergeWk.SaveAs FolderPath & "MergeData.xlsx"
    ' Close the merging workbook
    MergeWk.Close SaveChanges:=False
    Application.ScreenUpdating = True
    ' Display a message indicating the merging is complete
    MsgBox "Merged file is saved to D:\Temp\Data\MergeData.xlsx"
End Sub

相关问题