-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] handling of threaded comments #674
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JanMarvin
force-pushed
the
threadedComments
branch
from
July 3, 2023 22:12
281b74a
to
35048f1
Compare
JanMarvin
force-pushed
the
threadedComments
branch
from
July 4, 2023 08:42
0726e78
to
77bcf89
Compare
library(openxlsx2)
st_guid <- function() {
paste0(
"{",
openxlsx2:::random_string(length = 8, pattern = "[A-F0-9]"), "-",
openxlsx2:::random_string(length = 4, pattern = "[A-F0-9]"), "-",
openxlsx2:::random_string(length = 4, pattern = "[A-F0-9]"), "-",
openxlsx2:::random_string(length = 4, pattern = "[A-F0-9]"), "-",
openxlsx2:::random_string(length = 12, pattern = "[A-F0-9]"),
"}"
)
}
st_userid <- function() {
openxlsx2:::random_string(length = 16, pattern = "[a-z0-9]")
}
wb_add_persons <- function(wb, name) {
xml_person <- xml_node_create(
"person",
xml_attributes = c(
displayName = name,
id = st_guid(),
userId = st_userid(),
providerId = "None"
)
)
if (is.null(wb$persons)) {
wb$persons <- xml_node_create(
"personList",
xml_attributes = c(
`xmlns` = "http://schemas.microsoft.com/office/spreadsheetml/2018/threadedcomments",
`xmlns:x` = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
)
)
}
wb$persons <- xml_add_child(wb$persons, xml_person)
wb
}
wb_get_person <- function(name) {
openxlsx2:::rbindlist(xml_attr(wb$persons, "personList", "person"))
}
## create a base file
wb <- wb_workbook() %>% wb_add_worksheet()
c1 <- create_comment(text = "this is a comment", author = "")
wb$add_comment(dims = "A1", comment = c1)
wb$add_comment(dims = "B1", comment = c1)
wb$Content_Types <- append(
wb$Content_Types,
c(
"<Default Extension=\"vml\" ContentType=\"application/vnd.openxmlformats-officedocument.vmlDrawing\"/>",
"<Override PartName=\"/xl/threadedComments/threadedComment1.xml\" ContentType=\"application/vnd.ms-excel.threadedcomments+xml\"/>",
"<Override PartName=\"/xl/persons/person.xml\" ContentType=\"application/vnd.ms-excel.person+xml\"/>"
)
)
wb$worksheets_rels[[1]] <- append(
wb$worksheets_rels[[1]],
c(
"<Relationship Id=\"rId3\" Type=\"http://schemas.microsoft.com/office/2017/10/relationships/threadedComment\" Target=\"../threadedComments/threadedComment1.xml\"/>"
)
)
wb$workbook.xml.rels <- append(
wb$workbook.xml.rels,
"<Relationship Id=\"rId5\" Type=\"http://schemas.microsoft.com/office/2017/10/relationships/person\" Target=\"persons/person.xml\"/>"
)
wb <- wb %>%
wb_add_persons(name = "Jan Marvin") %>%
wb_add_persons(name = "John Doe")
wb_get_person()
#> displayName id providerId
#> 1 Jan Marvin {9F46CFFC-F611-73CA-3033-846B134496A9} None
#> 2 John Doe {8220D9CF-459E-2B92-2C65-9044D4BEF1C4} None
#> userId
#> 1 90i02y3av46fwuqk
#> 2 7oxa3pjty39hbk97
wb$threadComments <- "<ThreadedComments xmlns=\"http://schemas.microsoft.com/office/spreadsheetml/2018/threadedcomments\" xmlns:x=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"></ThreadedComments>"
c1_id <- st_guid()
c2_id <- st_guid()
c3_id <- st_guid()
# add a fictional comment and a reply
wb$threadComments[[1]] <- wb$threadComments[[1]] %>%
xml_add_child(
xml_child = c(
sprintf("<threadedComment ref=\"A1\" dT=\"2023-07-01T19:19:30.08\" personId=\"%s\" id=\"%s\"><text>Remember when I added threaded comments? Would be cool if we can have these in {openxlsx2}!</text></threadedComment>", wb_get_person()$id[2], c1_id),
sprintf("<threadedComment ref=\"A1\" dT=\"2023-07-02T19:19:30.08\" personId=\"%s\" id=\"%s\" parentId=\"%s\"><text>Yes, I do remember! Let's check this out.</text></threadedComment>", wb_get_person()$id[1], c2_id, c1_id),
sprintf("<threadedComment ref=\"A1\" dT=\"2023-07-02T19:19:30.08\" personId=\"%s\" id=\"%s\" parentId=\"%s\"><text>Yes, I do remember! Let's check this out.</text></threadedComment>", wb_get_person()$id[1], st_guid(), c1_id)
)
) %>%
xml_add_child(
xml_child = c(
sprintf("<threadedComment ref=\"B1\" dT=\"2023-07-01T19:19:30.08\" personId=\"%s\" id=\"%s\" done=\"1\"><text>Remember when I added threaded comments? Would be cool if we can have these in {openxlsx2}!</text></threadedComment>", wb_get_person()$id[2], c3_id)
)
)
# unfortunately we do not load the comment correctly
wb$comments[[1]][[1]] <- list(
ref = "A1",
author = sprintf("tc=%s", c1_id),
comment = "<t>open</t>",
style = FALSE
)
wb$comments[[1]][[2]] <- list(
ref = "B1",
author = sprintf("tc=%s", c3_id),
comment = "<t>done</t>",
style = FALSE
)
wb$add_data(dims = "A1", x = "open")
wb$add_data(dims = "B1", x = "done")
# wb$save("/tmp/test.xlsx")
# unlink("/tmp/test", recursive = TRUE)
# unzip("/tmp/test.xlsx", exdir = "/tmp/test")
if (interactive()) wb$open() |
JanMarvin
force-pushed
the
threadedComments
branch
from
July 16, 2023 14:31
77bcf89
to
e063dbd
Compare
JanMarvin
force-pushed
the
threadedComments
branch
from
July 16, 2023 19:05
0fcaaed
to
ee11b2b
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently there is no interface, but the following works. Therefore it should be straightforward to implement this. We could handle provider ids too.
Pinging: @JMPivette