Site icon Kaizen.Personal computer work.

Move Worksheet.(Microsoft Excel Office Scripts)

Japanese version.

Introduces how to move a worksheet using Office scripts.

Operations

The following syntax is used (Red text indicates variable parts).

workbook.getWorksheet("Moving sheet name").setPosition(Destination location);

The position to be moved is a sequential number starting from 0 (first).

Move to the top (leftmost)

If 0 is specified, it moves to the beginning.

// Move to the top
workbook.getWorksheet("Moving sheet name").setPosition(0);

Move to the end (rightmost)

The next designation moves to the bottom.

// Move to the end
workbook.getWorksheet("Moving sheet name").setPosition(workbook.getWorksheets.length - 1);

Moves to the front or back of the specified worksheet

To move to the front or back of a given worksheet, do the following

let targetSheet = workbook.getWorksheet("Moving sheet name");
let baseSheet = workbook.getWorksheet("Reference sheet name");
let before:boolean = True if moving before the reference sheet; false if moving after the reference sheet;

let targrtPosition = targetSheet.getPosition();
let basePosition = baseSheet.getPosition();
let offset: number;

if(before){
  if (basePosition < targrtPosition) {
    offset = 0;
  }
  else {
    offset = -1;
  }
}
else{
  if (basePosition > targrtPosition) {
    offset = 0;
  }
  else {
    offset = 1;
  }
}

targetSheet.setPosition(
  basePosition + offset
);

For those who want to learn Office script effectively

The information on this site is now available in an easy-to-read e-book format.

Or Kindle Unlimited (unlimited reading).

You willl discover how to about basic operations.

By the end of this book, you will be equipped with the knowledge you need to use Excel Office Script to streamline your workflow.

Links

Office Scripts Articles

Exit mobile version