How to automatically transfer data between two worksheets using Office Scripts based on user input

jeff88

New Member
Joined
Jan 25, 2024
Messages
16
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hello,

I am trying to setup a worksheet with two sheets, one that has a "Jobs" sheet and one with a "Completed Jobs" sheet. On the "Jobs" sheet I will have rows of data and the column at the end will have a dropdown menu with a "Complete" option. I want a user to be able to select the "Complete" option and Excel automatically moves that row over to the "Completed Jobs" sheet.

I am using the web app as this is for multiple users so I need to use Office Scripts.

@skillilea got me started with some basic code (thank you skillilea) but I need to figure out the rest of the code to get it to work

JavaScript:
function main(workbook: ExcelScript.Workbook) {
  let sourceSheet = workbook.getWorksheet("Sheet1");
  let targetSheet = workbook.getWorksheet("Sheet2");
 
  let sourceRange = sourceSheet.getRange("A1:C10");
  let valuesToTransfer = sourceRange.getValues();
 
  let targetRange = targetSheet.getRange("A1");
 
  targetRange.setValues(valuesToTransfer);
}
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Bumping this as I'm trying to get this to work still. Does anybody know Office Scripts that can assist in the code I would need to make it work?
 
Upvote 0
Hello,

I am trying to setup a worksheet with two sheets, one that has a "Jobs" sheet and one with a "Completed Jobs" sheet. On the "Jobs" sheet I will have rows of data and the column at the end will have a dropdown menu with a "Complete" option. I want a user to be able to select the "Complete" option and Excel automatically moves that row over to the "Completed Jobs" sheet.

I am using the web app as this is for multiple users so I need to use Office Scripts.

@skillilea got me started with some basic code (thank you skillilea) but I need to figure out the rest of the code to get it to work

JavaScript:
function main(workbook: ExcelScript.Workbook) {
  let sourceSheet = workbook.getWorksheet("Sheet1");
  let targetSheet = workbook.getWorksheet("Sheet2");
 
  let sourceRange = sourceSheet.getRange("A1:C10");
  let valuesToTransfer = sourceRange.getValues();
 
  let targetRange = targetSheet.getRange("A1");
 
  targetRange.setValues(valuesToTransfer);
}
Hello,
I have read your previous post with the sample data you provided, so that I suggest the following script:
JavaScript:
function main(workbook: ExcelScript.Workbook) {
  const sourceWs = workbook.getWorksheet("Jobs");
  const targetWs = workbook.getWorksheet("Completed Jobs");
  if (sourceWs && targetWs) {
    let lastRow = sourceWs.getUsedRange().getRowCount();
    const range = sourceWs.getRange(`A1:E${lastRow}`);
    const result = doWork(range);
    if (result.length > 0) {
      const targetWsLastRow = targetWs.getUsedRange().getRowCount();
      const rowCount = result.length;
      const columnCount = result[0].length;
      const destinationRange = targetWs.getRangeByIndexes(targetWsLastRow, 0, rowCount, columnCount);
      destinationRange.setValues(result);
    }
  }
}

function doWork(range: ExcelScript.Range): (string | number | boolean)[][] {
  const values = range.getValues();
  const rowCount = values.length;
  const columnCount = values[0].length;
  const result: (string | number | boolean)[][] = [];
  for (let i = rowCount -1; i > 0; i--) {
    const row = values[i];
    if (row[4] == "Yes") {
      result.push(row.slice(0, -1));
      const rowRange = range.getRow(i);
      rowRange.delete(ExcelScript.DeleteShiftDirection.up);
    }
  }
  return result;
}
Before running script,
1709090957470.png

After running script,
1709091008365.png

1709091116629.png

Remember to modify the script to suit your need.
Hope this will help.
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,161
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top