Skip to content
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

[drawings] improve relationship id selection #838

Merged
merged 5 commits into from
Nov 2, 2023
Merged

Conversation

JanMarvin
Copy link
Owner

This fixes a bug with relationship ids. If the sheet already contains drawings and some of these drawings have been removed, try to get the next free relationship id and do not duplicate existing relationship ids.

Also this PR improves the unfold_cols() code so that the column widths selection is now faster in some corner cases.

library(openxlsx2)

# https://drive.google.com/file/d/11KvtmfUJo2IFCHam_QlstsDWkg4J_SVr/view?usp=drive_link

tabelapronta <- structure(
  list(
    Data = c(45224, 45224, 45224, 45224, 45224, 45224, 45224, 45224, 45224, 45224),
    Estado = c("ms", "ms", "ms", "ms", "ms", "ms", "ms", "ms", "ms", "ms"),
    `Município` = c("campo-grande", "campo-grande", "campo-grande", "campo-grande", "campo-grande", "campo-grande", "campo-grande", "campo-grande", "campo-grande", "campo-grande"),
    `Endereço` = c("Avenida Rachid Neder", "Rua Quinze de Novembro", "Rua Brisas de Zaragoza", "Avenida Afonso Pena", "Avenida Nelly Martins", "Rua Assunção Borba", "Rua Goiás", "Rua Jeriba", NA, "Avenida Murilo Rolim Júnior"),
    `Descrição` = c("Apartamento contendo sala para dois ambientes ", "Apartamento residencial para VendaJardim dos Estados Campo Grande03", "KW IMVEIS VENDETRANSFERNCIA NO VILLAS DE CASTILLAS IIApartamento", "Excelente apartamento: Entrar e morar 2 quartos com dependncias ", "Lindo apartamento com 3 suites sala estendida com armrios embutidos ", "NOVA LIMA Apartamento trreo2 quartosSala amplaCozinha individualizada pia em granitolavanderia1 vaga de garagem ", "KW IMVEIS VENDE:Apartamento no Eudes Costa localizao privilegiada","J imaginou morar em um condomnioque conta com mais de 10 itens na rea de","O apartamento no bairro Jardim Petrpolis", NA),
    Links = c("https://www.zapimoveis.com.br/imovel/venda-apartamento-3-quartos-com-piscina-monte-castelo-campo-grande-ms-70m2-id-2519557992/", "https://www.zapimoveis.com.br/imovel/venda-apartamento-3-quartos-mobiliado-jardim-dos-estados-campo-grande-ms-210m2-id-2514075379/", "https://www.zapimoveis.com.br/imovel/venda-apartamento-2-quartos-com-churrasqueira-mata-do-segredo-campo-grande-ms-60m2-id-2591333698/", "https://www.zapimoveis.com.br/imovel/venda-apartamento-2-quartos-com-ar-condicionado-centro-campo-grande-ms-120m2-id-2434759153/", "https://www.zapimoveis.com.br/imovel/venda-apartamento-3-quartos-mobiliado-vila-margarida-campo-grande-ms-109m2-id-2506388498/", "https://www.zapimoveis.com.br/imovel/venda-apartamento-2-quartos-com-churrasqueira-nova-lima-campo-grande-ms-48m2-id-2663643060/", "https://www.zapimoveis.com.br/imovel/venda-apartamento-2-quartos-jardim-dos-estados-campo-grande-ms-60m2-id-2663878870/", "https://www.zapimoveis.com.br/imovel/venda-apartamento-3-quartos-com-piscina-chacara-cachoeira-campo-grande-ms-75m2-id-2607793591/", "https://www.zapimoveis.com.br/imovel/venda-apartamento-2-quartos-vila-margarida-campo-grande-ms-88m2-id-2663939194/", "https://www.zapimoveis.com.br/imovel/venda-apartamento-2-quartos-com-jardim-jardim-petropolis-campo-grande-ms-52m2-id-2663628498/"),
    Coordenadas = c("-20.440178,-54.610673", "-20.461933,-54.59461", "-20.411966,-54.595724", "Avenida Afonso Pena - Centro, Campo Grande - MS", "-20.435444,-54.580942", "-20.394701,-54.573465", "-20.456606,-54.596687", "-20.470381,-54.581569", "Vila Margarida, Campo Grande - MS", "-20.455429,-54.669144"),
    valor = c(360000, 3e+06, 120000, 4e+05, 1050000, 136000, 145000, 3e+05, 560000, 155000),
    area = c(52, 218, 162, 60, 240, 67, 100, 59, 60, 73),
    quartos = c(2, 2, 4, 2, 6, 2, 3, 1, 2, 2),
    vagas = c(1, 2, 2, 1, 2, 2, 1, 1, 1, 2), 
    banheiros = c(1, 5, 5, 1, 6, 2, 2, 1, 1, 2), 
    vunit = c(4615.38, 7339.45, 4876.54, 3166.67, 6208.33, 3805.97, 3150, 1355.93, 5333.33, 3287.67)
  ),
  row.names = c(NA, 10L),
  class = "data.frame"
)
# head(tabelapronta)

wb <- wb_load("/tmp/files/arq_imoveis/resultados.xlsm")
# wb <- wb_workbook()$add_worksheet("dados_brutos")
wb$add_data(x = tabelapronta, sheet = "dados_brutos", start_row = 7, row_names = FALSE,col_names = FALSE)


fldrs <- dir("/tmp/files/arq_imoveis", full.names = TRUE)
names(fldrs) <- as.numeric(gsub("\\D+", "", fldrs)) - 100
for (i in seq_len(nrow(tabelapronta))) {
  numfotos <- dir(fldrs[[i]], pattern = "*.jpg", full.names = TRUE)
  for (j in seq_along(numfotos)) {
    wb$add_image(sheet = 1, dims = wb_dims(i+6,13+j), file = numfotos[j], width = 7.28, height = 3.64, units = "cm")   
  }
}

cols <- int2col(14:(ncol(tabelapronta)+5))
rows <- 7:(nrow(tabelapronta)+6)

wb$set_col_widths(sheet=1, cols = cols, widths = 38) # no longer very slow!
wb$set_row_heights(sheet=1, rows = rows, heights = 105)

wb$add_cell_style(
  sheet = 1,
  dims = wb_dims(rows = 7:(nrow(tabelapronta)+6), cols = 1:ncol(tabelapronta)),
  horizontal = "center",
  vertical = "center"
)

if (interactive()) wb$open()

# nomecomp <- "resultados2.xlsm"
# wb_save(wb,nomecomp,overwrite=TRUE)

@JanMarvin JanMarvin merged commit 128ff77 into main Nov 2, 2023
9 checks passed
@JanMarvin JanMarvin deleted the gh_issue_837 branch November 2, 2023 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant