Yes, at the beginning it's normal. If you have some time I highly recommend reading through the first chapters of R for data science, the book structure makes it more efficient than googling around problem after problem.
Both nb_sheets and content are defined in the script. The <- operator assigns a variable, could be replaced by = as in most other languages.
Sorry, my previous script was inexact as I aimed to give a general idea of the process. Here is a much better one that works on a real file:
library(openxlsx) # so that we don't need to write it everytime
# define file names
input_path <- "my_input.xlsx"
new_filename <- "my_output.xlsx"
# what are the names of the sheets in that file? We get a vector
sheet_names <- getSheetNames(input_path)
sheet_names
#> [1] "Sheet1" "Sheet2" "Sheet3"
# Initialize an output workbook. We store it in the 'wb' object
wb <- createWorkbook()
wb
#> A Workbook object.
#>
#> Worksheets:
#> No worksheets attached
# Now let's select the first sheet
cur_sheet <- sheet_names[1]
# Read its contents
content <- read.xlsx(input_path, sheet = cur_sheet)
content
#> colA1 colB1
#> 1 170 1.636704
#> 2 80 1.299625
#> 3 42 1.157303
#> 4 93 1.348315
# We can do whatever operations we want:
content$colA1 <- content$colA1 + 1
# And add it to the Workbook we are storing in "wb"
addWorksheet(wb = wb, sheetName = cur_sheet)
writeData(wb, sheet = cur_sheet, x = content)
# Note that at this point, we are only modifying the "wb" object, we haven't actually written anything to disk
# When we are finished, we can save the whole "wb" object at once
saveWorkbook(wb, new_filename)
Now that's for a single sheet, but that let's you do things step by step and look inside the objects at each step to understand what's happening. Once you want to do it for all sheets, you can use a for loop:
for(cur_sheet in sheet_names){
...
}
At each iteration of the loop, cur_sheet will take the next value from sheet_names. So it's equivalent to do:
cur_sheet <- sheet_names[1]
# do stuff
cur_sheet <- sheet_names[2]
# do stuff
cur_sheet <- sheet_names[3]
...
And finally, it's very informative to look at the documentation of any function. You can get it easily with ?name_of_function. For example, try ?saveWorkbook.