sheet_add_aoa Creates a new worksheet in the workbook #3105

Closed
opened 2024-04-07 15:06:47 +00:00 by kriptcs · 1 comment

Greetings! At this moment I have an application that receives an AOA from a form and takes that data to create a worksheet. The problem is that every time I submit new data through the form it creates a separate sheet within the workbook instead of adding the data to the existing worksheet.

My Code

var wb = XLSX.utils.book_new();

function FormScreen({ route, navigation }) {
  const { control, handleSubmit } = useForm();

  async function onSubmit(data) {
    var dataArray = Object.entries(data); 

    const wbout = XLSX.write(wb, {
      type: "base64",
      bookType: "xlsx",
    });

    var ws = XLSX.utils.json_to_sheet(dataArray); 

    XLSX.utils.sheet_add_aoa(ws, dataArray);

    XLSX.utils.book_append_sheet(wb, ws); 

    const uri = FileSystem.cacheDirectory + "form.xlsx"; 

    await FileSystem.writeAsStringAsync(uri, wbout, {
      encoding: FileSystem.EncodingType.Base64,
    });

    await Sharing.shareAsync(uri, {
      mimeType:
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      dialogTitle: "MyWater data",
      UTI: "com.microsoft.excel.xlsx",
    });
  }

I have tried working with var ws but the app doesn't work at all if I mess with it. What am I doing wrong? Thank you in advance for the help!

Greetings! At this moment I have an application that receives an AOA from a form and takes that data to create a worksheet. The problem is that every time I submit new data through the form it creates a separate sheet within the workbook instead of adding the data to the existing worksheet. My Code ``` var wb = XLSX.utils.book_new(); function FormScreen({ route, navigation }) { const { control, handleSubmit } = useForm(); async function onSubmit(data) { var dataArray = Object.entries(data); const wbout = XLSX.write(wb, { type: "base64", bookType: "xlsx", }); var ws = XLSX.utils.json_to_sheet(dataArray); XLSX.utils.sheet_add_aoa(ws, dataArray); XLSX.utils.book_append_sheet(wb, ws); const uri = FileSystem.cacheDirectory + "form.xlsx"; await FileSystem.writeAsStringAsync(uri, wbout, { encoding: FileSystem.EncodingType.Base64, }); await Sharing.shareAsync(uri, { mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", dialogTitle: "MyWater data", UTI: "com.microsoft.excel.xlsx", }); } ``` I have tried working with `var ws` but the app doesn't work at all if I mess with it. What am I doing wrong? Thank you in advance for the help!
Owner

https://docs.sheetjs.com/docs/api/utilities/array#array-of-arrays-input

sheet_add_aoa and sheet_add_json take an options argument.

By default, the methods start writing on cell A1, so repeated calls will look like overwriting. You can pass the option origin: -1 to append data to the worksheet (starting on the row after the last row)

https://docs.sheetjs.com/docs/api/utilities/array#array-of-arrays-input `sheet_add_aoa` and `sheet_add_json` take an options argument. By default, the methods start writing on cell A1, so repeated calls will look like overwriting. You can pass the option `origin: -1` to append data to the worksheet (starting on the row after the last row)
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#3105
No description provided.