library(xlsx)
passWD <- "Microsoft365"
wb.df <- loadWorkbook(file="./data/chap8data1.xlsx",
                      password = passWD)
my.sheets <- getSheets(wb.df)
print(names(my.sheets))

    #-- create a range of cells to receive the title

tit.rows <- createRow(my.sheets[[2]], rowIndex=1:4)
tit.cells <- createCell(row=tit.rows, colIndex=3:8)

    #-- write title in the first 3 rows and save workbook 

setCellValue(tit.cells[[1,1]],
             "Table 1: Mean Ratings by Group and Target, and")
setCellValue(tit.cells[[2,2]],
             "Group-level standard deviations")
setCellValue(tit.cells[[3,1]],"(Author: Kilem L. Gwet)")

saveWorkbook(wb.df, file="./data/chap8data2.xlsx")

    #-- change the first 6 column labels in the first row

wb.df <- loadWorkbook(file="./data/chap8data2.xlsx")
my.sheets <- getSheets(wb.df)
print(names(my.sheets))
clabels.rows <- getRows(sheet = my.sheets[["summaryStats2"]],
                        rowIndex=5)
clabs.cells <- getCells(row=clabels.rows,colIndex = 3:8)
setCellValue(clabs.cells[["5.3"]],"Section")
setCellValue(clabs.cells[["5.4"]],"Subject")
setCellValue(clabs.cells[["5.5"]],"Judge1")
setCellValue(clabs.cells[["5.6"]],"Judge2")
setCellValue(clabs.cells[["5.7"]],"Judge3")
setCellValue(clabs.cells[["5.8"]],"Judge4")

    #-- Add comment line in row #12 

sub1.rows <- getRows(sheet = my.sheets[[2]], rowIndex=6)
sub1.cells <- getCells(row=sub1.rows,colIndex = 3:12)
ct.rows <- createRow(my.sheets[[2]], rowIndex=12)
ct.cells <- createCell(row=ct.rows, colIndex=1:8)
j3score <- getCellValue(sub1.cells[["6.7"]])
footnote = paste0("Score that Judge3 assigned to subject 1 is: ", 
                  j3score)
setCellValue(ct.cells[[1,3]],footnote)

saveWorkbook(wb.df, file="./data/chap8data3.xlsx")

