Automatically Move Completed Tasks to an Archive (with Excel Office Scripts & Power Automate)

Automatically Move Completed Tasks to an Archive (with Excel Office Scripts & Power Automate)

Leila Gharani

1 год назад

240,116 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Shreysanthu
Shreysanthu - 08.11.2023 04:52

Subscribed from india

Ответить
AhMauy Mun
AhMauy Mun - 12.10.2023 04:39

Can you make Excel shortcuts on coffee mugs to sell? So I can buy some.

Ответить
Anbuselvam Kovilmani
Anbuselvam Kovilmani - 10.08.2023 17:59

I tried changing the office script as per my requirements. I have two criteria and two target columns to move the rows of data. Column 5 "Done" is to be moved table "Archives" and same column "Waiting" to be moved Table "Archives2" which I added newly. For both case Column1 Value not Equal to "0" So totally Column5 and Column1 to be match the criteria then moved data to be respective tables. I used ChatGPT but it moves both Criteria to Archives2 Table. Can anyone Change the Office Script?

function main(workbook: ExcelScript.Workbook) {
// You can change these names to match the data in your workbook.
const SOURCE_TABLE_NAME = "Tasks";

// Get the Table object.
let sourceTable = workbook.getTable(SOURCE_TABLE_NAME);

// If the source table is missing, report that information and stop the script.
if (!sourceTable) {
console.log(
`Source table missing - Check to make sure the source table (${SOURCE_TABLE_NAME}) is present before running the script.`
);
return;
}

// Save the filter criteria currently on the source table.
const originalTableFilters = {};
// For each table column, collect the filter criteria on that column.
sourceTable.getColumns().forEach((column) => {
let originalColumnFilter = column.getFilter().getCriteria();
if (originalColumnFilter) {
originalTableFilters[column.getName()] = originalColumnFilter;
}
});

// Get all the data from the table.
const sourceRange = sourceTable.getRangeBetweenHeaderAndTotal();
const dataRows: (
| number
| string
| boolean
)[][] = sourceTable.getRangeBetweenHeaderAndTotal().getValues();

// Create variables to hold the rows to be moved and their addresses.
let rowsToMoveValues: (number | string | boolean)[][] = [];
let rowAddressToRemove: string[] = [];

// Get the data values from the source table.
for (let i = 0; i < dataRows.length; i++) {
const column5Value = dataRows[i][5]; // Column 5 value
const column1Value = dataRows[i][1]; // Column 1 value

if (
((column5Value === "Done" && column1Value !== 0) || // Check Column 5 value for "Done"
(column5Value === "Waiting" && column1Value !== 0)) && // Check Column 5 value for "Waiting"
column1Value !== 0 // Check Column 1 value
) {
rowsToMoveValues.push(dataRows[i]);

// Get the intersection between table address and the entire row where we found the match. This provides the address of the range to remove.
let address = sourceRange
.getIntersection(sourceRange.getCell(i, 0).getEntireRow())
.getAddress();
rowAddressToRemove.push(address);
}
}

// If there are no data rows to process, end the script.
if (rowsToMoveValues.length < 1) {
console.log(
"No rows selected from the source table match the filter criteria."
);
return;
}

let targetTableName = "Archives"; // Default target table name

// Check if Column 5 value is "Waiting" to set the target table name
if (rowsToMoveValues.some(row => row[5] === "Waiting")) {
targetTableName = "Archives2";
}

console.log(`Adding ${rowsToMoveValues.length} rows to target table "${targetTableName}".`);

// Get the Table object for the target table.
let targetTable = workbook.getTable(targetTableName);

// If the target table is missing, report that information and stop the script.
if (!targetTable) {
console.log(`Target table "${targetTableName}" is missing.`);
return;
}

// Insert rows at the end of target table.
targetTable.addRows(-1, rowsToMoveValues);

// Remove the rows from the source table.
const sheet = sourceTable.getWorksheet();

// Remove all filters before removing rows.
sourceTable.getAutoFilter().clearCriteria();

// Important: Remove the rows starting at the bottom of the table.
// Otherwise, the lower rows change position before they are deleted.
console.log(
`Removing ${rowAddressToRemove.length} rows from the source table.`
);
rowAddressToRemove.reverse().forEach((address) => {
sheet.getRange(address).delete(ExcelScript.DeleteShiftDirection.up);
});

// Reapply the original filters.
Object.keys(originalTableFilters).forEach((columnName) => {
sourceTable
.getColumnByName(columnName)
.getFilter()
.apply(originalTableFilters[columnName]);
});
}

Ответить
Eric The Bro Bean!
Eric The Bro Bean! - 01.07.2023 08:25

Im putting Leila's videos into my ASMR playlist because of the calming voice 😴

Ответить
nagarajan kanagam
nagarajan kanagam - 23.06.2023 12:51

HI, your videos are awesome, I have tried to refresh connection through automate but while run code nothing happened could you guide me

Ответить
Bhavik Khatri
Bhavik Khatri - 15.06.2023 21:29

Awesome

Ответить
Jean Poulin
Jean Poulin - 12.06.2023 00:34

Hi Leila,

When are you going to create a course related to Office Scripts?

Ответить
Martin Stoilov
Martin Stoilov - 07.06.2023 23:19

I haven't got much hope for Automate if you have to rewrite 50% of the code.

Ответить
Aayush Sharma
Aayush Sharma - 06.06.2023 21:18

Everyone making video on How to use office scripts, but not found a single video on "How to enable automate tab in excel"?

Ответить
Bernard Simmonds
Bernard Simmonds - 23.05.2023 10:15

Great Video Leila. As an exercise I changed it to be a To-Do-List for me.
I also added a sheet that has repetitive tasks that are automatically added to task list.
Each item on this list has a date for when the tasks are to be added.
I am in Australia, so I use the Australian date format dd/mm/yyyy
The script works fine when I run it but when it is scheduled and run from a 'flow' the dates are interpreted as mm/dd/yyyy it adds tasks based on that date format.
I've changed every date setup/option on OneDrive and Sharepoint that I can find, and it masks no difference.
Does anyone have any suggestions?

Ответить
Piotr VSA
Piotr VSA - 04.05.2023 11:39

Hi Leila, how to add the Atomate option to top row ribbon on Excel? Can’t find it in desktop nor web version of Excel for Win10. MS search also has not helped on it. Thx for info about it.

Ответить
Chris Blythe
Chris Blythe - 01.05.2023 11:09

Cool tip, but i just hide the done tasks using a filter!

Ответить
weyikun fekadike
weyikun fekadike - 29.04.2023 00:37

your presentation is beautiful and clear, thank you!

Ответить
Christopher Seater
Christopher Seater - 26.04.2023 14:46

HI Leila, it looks like MS have added a script into excel as an example of this. "Move Rows Between Tables"

/*
This script does the following:

Selects rows from the source table where the value in a column is equal to some value (FILTER_VALUE in the script).
Moves all selected rows into the target table in another worksheet.
Reapplies the relevant filters to the source table.
*/

function main(workbook: ExcelScript.Workbook) {
// You can change these names to match the data in your workbook.
const TARGET_TABLE_NAME = "Archive";
const SOURCE_TABLE_NAME = "Current";

// Select what will be moved between tables.
const FILTER_COLUMN_INDEX = 3;
const FILTER_VALUE = "Done";

// Get the Table objects.
let targetTable = workbook.getTable(TARGET_TABLE_NAME);
let sourceTable = workbook.getTable(SOURCE_TABLE_NAME);

// If either table is missing, report that information and stop the script.
if (!targetTable || !sourceTable) {
console.log(
`Tables missing - Check to make sure both source (${TARGET_TABLE_NAME}) and target table (${SOURCE_TABLE_NAME}) are present before running the script. `
);
return;
}

// Save the filter criteria currently on the source table.
const originalTableFilters = {};
// For each table column, collect the filter criteria on that column.
sourceTable.getColumns().forEach((column) => {
let originalColumnFilter = column.getFilter().getCriteria();
if (originalColumnFilter) {
originalTableFilters[column.getName()] = originalColumnFilter;
}
});

// Get all the data from the table.
const sourceRange = sourceTable.getRangeBetweenHeaderAndTotal();
const dataRows: (
| number
| string
| boolean
)[][] = sourceTable.getRangeBetweenHeaderAndTotal().getValues();

// Create variables to hold the rows to be moved and their addresses.
let rowsToMoveValues: (number | string | boolean)[][] = [];
let rowAddressToRemove: string[] = [];

// Get the data values from the source table.
for (let i = 0; i < dataRows.length; i++) {
if (dataRows[i][FILTER_COLUMN_INDEX] === FILTER_VALUE) {
rowsToMoveValues.push(dataRows[i]);

// Get the intersection between table address and the entire row where we found the match. This provides the address of the range to remove.
let address = sourceRange
.getIntersection(sourceRange.getCell(i, 0).getEntireRow())
.getAddress();
rowAddressToRemove.push(address);
}
}

// If there are no data rows to process, end the script.
if (rowsToMoveValues.length < 1) {
console.log(
"No rows selected from the source table match the filter criteria."
);
return;
}

console.log(`Adding ${rowsToMoveValues.length} rows to target table.`);

// Insert rows at the end of target table.
targetTable.addRows(-1, rowsToMoveValues);

// Remove the rows from the source table.
const sheet = sourceTable.getWorksheet();

// Remove all filters before removing rows.
sourceTable.getAutoFilter().clearCriteria();

// Important: Remove the rows starting at the bottom of the table.
// Otherwise, the lower rows change position before they are deleted.
console.log(
`Removing ${rowAddressToRemove.length} rows from the source table.`
);
rowAddressToRemove.reverse().forEach((address) => {
sheet.getRange(address).delete(ExcelScript.DeleteShiftDirection.up);
});

// Reapply the original filters.
Object.keys(originalTableFilters).forEach((columnName) => {
sourceTable
.getColumnByName(columnName)
.getFilter()
.apply(originalTableFilters[columnName]);
});
}

Ответить
Eino Consult
Eino Consult - 25.04.2023 18:22

Leila, at the end you added some code so that if there is no DONE the script will still work. However, not having DONE makes the flow fails in Power Automate, any clue to fix this ?

Ответить
Engle Ville
Engle Ville - 19.04.2023 11:08

huhu i wanna learn all this but where to start? help

Ответить
Daniel Blois
Daniel Blois - 18.04.2023 21:09

There are 2 videos I would love if you would make (if you haven't already). 1) Show us things you can do in Office Scripts that you cannot do in Excel 2) Show us how to integrate office scripts with Excel if possible. I am very experienced with VBA already but I haven't touched office scripts yet.

Ответить
CA Nirmal Choudhary
CA Nirmal Choudhary - 18.04.2023 18:42

This is altogether new school of learning and I got automate tab but no record button on desktop version. All are afraid of automation

Ответить
ericsson test
ericsson test - 18.04.2023 17:22

..

Ответить
Rafa
Rafa - 18.04.2023 05:08

The office 365 family have that feature?

Ответить
Sapi Software
Sapi Software - 17.04.2023 16:59

Hi Leila, please do a course for Office Scripts!!! regards!

Ответить
Abdullah Rajab
Abdullah Rajab - 17.04.2023 13:05

can i total every page when printting
please help in excel

Ответить
Abhishek mor
Abhishek mor - 17.04.2023 06:23

Record button missing

Ответить
TheHellis
TheHellis - 17.04.2023 06:14

Since there is a loop at the end I would remove the filtering and just do it all in the loop.
That way you make the code more efficient and you wouldn't need to worry about the error

Ответить
bouchareb yasmine
bouchareb yasmine - 16.04.2023 16:48

I love you leila ❤

Ответить
Ji Hu
Ji Hu - 16.04.2023 10:48

Should advise the client to use Microsoft planner.

Ответить
Nononono
Nononono - 15.04.2023 19:30

Plz 🙏forgive for all offenses 🙏 lela

Ответить
Shamie Seenarine
Shamie Seenarine - 15.04.2023 19:03

Hi Leela did y tech class privately

Ответить
Andrey Dementyev
Andrey Dementyev - 15.04.2023 18:48

Yeah, brilliant, so you need to be a coder to create a task manager, bravo Microsoft, we are all programmers 😖 Or you can just keep all records at the same table and filter rows on all except with a done status

Ответить
Abu Geda
Abu Geda - 15.04.2023 18:42

you are so different Leila, thanks for capacitating us dear, apricated love to u

Ответить
abhishek kuradia
abhishek kuradia - 14.04.2023 22:09

Thank you, I can relate this more with Javascript syntax finally.

Ответить
FluffyBasket
FluffyBasket - 14.04.2023 19:25

Hi Leila! Does this replace VBA?

Ответить
iamthemoss
iamthemoss - 13.04.2023 18:13

Leila, you are simply, "the bomb" you have helped endless people. Thank you.

Ответить
Yves Seybel
Yves Seybel - 13.04.2023 17:02

Great video Leila, and nice work figuring this out! So, it looks like people who want to automate tasks and do not know VBA end up automating them using...hmm...well.. something that looks like VBA!! Now, I am curious to see how some more complex tasks can be written in both Automate and VBA and check which of the two codes is more efficient. Does Microsoft have some hidden agenda to eliminate VBA at some point? Ouch! that would hurt... From my side, I will continue to play in my VBA sandbox for the foreseeable future :) Thank you again for this great content.

Ответить
HouseJug
HouseJug - 13.04.2023 16:52

I’d so love to know what the ‘make it rock’ script does 😂

Ответить
Massimo Cafagna
Massimo Cafagna - 13.04.2023 10:37

Number one Leila!

Ответить
Hackentertainment
Hackentertainment - 13.04.2023 08:50

Sorry but there is no automate tab in the excel desktop... it's only for office 365

Ответить
Murad
Murad - 13.04.2023 07:53

Difficult for me

Ответить
Calebe Belo
Calebe Belo - 12.04.2023 22:33

It is the most intricated thing I have seen

Ответить
Ka Aa
Ka Aa - 12.04.2023 19:41

Thank you leila so muchhhhhhhhhh!!! My company do flayers for party as an invitation. we usually send by emails. is there any way that we can do it online where they can view the information. will you please, do tutorial on Microsoft Sway on Microsoft 360 on how to do a flyer. Thanks in advance!!!

Ответить
NandKishore Yadav
NandKishore Yadav - 12.04.2023 18:28

Leila, The explanation was very clear, like the new method and will try this once..
I liked the video editing as well, May I know what tool do u use to edit videos?

Ответить
smbs47
smbs47 - 12.04.2023 02:05

Great video once again! Does Office Spripts have interactive debug tools like VBA with things like the immediate window, watch, local variables and breakpoints?

Ответить
Sumesh N
Sumesh N - 11.04.2023 15:36

Was this not already in the macro

Ответить
SuperRice2010
SuperRice2010 - 11.04.2023 07:59

Is it possible to do it without vba or script?

Ответить
Jyoti Prasad Chaudhary
Jyoti Prasad Chaudhary - 11.04.2023 01:12

Super cool trick, I have some queries. For example if my formula is A2+Sheet2!A1+Sheet3!B5, CTRL +[ will take me to A2 cell but then how do i move to Sheet2!A1 and Sheet3!B5?

Ответить
Gowthaman
Gowthaman - 10.04.2023 23:30

Hi Leila, Thanks For wonderful Video, Is it possible to use other workbooks in office script?

Ответить