0

I'm trying to insert some rows to an Excel sheet which doesn't contain any tables, but I'm only able to insert blank rows. Attempting to change the values of these empty rows results in row.values printing the correct data, but the rows on the file itself are still blank, even after context.sync().

I have tried using the insert method from Excel.Range to insert an empty row successfully by doing:

let empty_space   = sheet.getCell(index,0).getResizedRange(index, row.length)
empty_space.insert("Down")

I then tried using the reference to the new range empty_space to replace its values with those on an array called row (whose length was used to define the range above):

empty_space.load("values")
await context.sync()
empty_space.values[0] = row 

If I then do console.log(empty_space.values), I see the correct values are listed, but the row in the file is still empty.

I also tried inserting the row into the sheet's used range via splice:

full_range.values.splice(index, 0, row)

Which does the same as above, printing correctly on the console but displaying empty cells.

How can I insert an array of values into a new row on top of the file, using Ranges. I can't convert the range to a table, as that creates a lot of other problems.

EDIT: Below is the full content of the function I'm using to try achieve this:

insert_banner = async ( sheet_range, context ) => {
   let banner_rows = this.state.sheet_content.banner.banner_rows

    for (let r in banner_rows) {
      let index         = parseInt(r)
      let row           = banner_rows[r]

      let empty_space   = this.state.sheet.getCell(index,0).getResizedRange(index, row.length)
      empty_space.insert("Down")
      empty_space.values[0] = row
      await context.sync()
    }
}

banner_rows is a 2-dimensional array.

EDIT 2: I tried using the set method of Excel.Range like this:

empty_space.set( {values: row} )

But regardless of what I try, I keep getting the following error:

"InvalidArgument: The number of rows or columns in the input array doesn't match the size or dimensions of the range."

Even though while debugging I can see that both row and empty_space.values do have the same dimensions, 1x48.

EDIT 3: Here's another approach I tried:

insert_banner = async ( sheet_range, context ) => {
  let banner_rows  = this.state.sheet_content.banner.banner_rows
  let sheet_values = sheet_range.values

  for (let r in banner_rows) {
    let index = parseInt(r)
    let row   = banner_rows[r]
    sheet_values.splice(index, 0, row)
  }

  return context.sync()
}

In this case, if I add a breakpoint after this function, I can see that sheet_ranges values do contain the correct information on the corresponding rows. I don't understand why this isn't being reflected in the file itself.

1 Answer 1

1

A couple of problems I see in your code:

  1. You do not need to load the values of empty_space. You need to load properties that your code is going to read after the sync, not properties your code is going to write to.

  2. When you assign values to empty_space.values[0], you are only populating the proxy object in your JavaScript, not the actual range in the workbook. To send the new values down to the workbook, you have to call context.sync. (Since it looks like you don't need the context.sync that's above this line, just move it below the line.) The reason that the context.log works is because you are logging the values of the proxy object.

I think the code in your second code block should be:

empty_space.values[0] = row
await context.sync()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, Rick. Unfortunately, I get the following error if I don't load the values: "PropertyNotLoaded: The property 'values' is not available. Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context." I'm going to edit my question and post the full function I'm using.
Your question has evolved. You originally said the workbook wasn't being updated. Your latest version says you are getting an error that you didn't mention before: "Invalid argument: The number of rows or columns in the input array doesn't match the size or dimensions of the range.". Also, as near as I can tell the latest version of your code uses the advice I gave and it does not load values, so your comment to my answer is confusing. I suggest that you start over with a new question. Provide the entire Excel.run method and specify the first error you get and where you get it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.