Aug 29 2023 08:07 PM
Hi, I am trying to auto fill a row of data that changes every month. right now the script is as below. I have bolded where I need help. the auto fill range is set to go to d11203 but the script will need to run on sheets that I do not know the end of the range so it is unusable unless I can fill the data all the way down. can someone please help me? All I've found is VBA script help and I keep getting errors when I add in code because it is different. I did this from the record actions in the automate tab. Thank you!
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
// Insert at range D:D on selectedSheet, move existing cells right
selectedSheet.getRange("D:D").insert(ExcelScript.InsertShiftDirection.right);
// Set range D1:D2 on selectedSheet
selectedSheet.getRange("D1:D2").setFormulasLocal([["Duration_Minutes"],["=(E2/60)"]]);
// Set number format for range D:E on selectedSheet
selectedSheet.getRange("D:E").setNumberFormatLocal("General");
// Set number format for range D2 on selectedSheet
selectedSheet.getRange("D2").setNumberFormatLocal("0.00");
// Auto fill range
selectedSheet.getRange("D2").autoFill("D2:D11203", ExcelScript.AutoFillType.fillDefault);
}
Aug 30 2023 04:00 AM
SolutionIf I understood correctly initially you have some data in column D. You shift it to the right, applied some formulae and formats and autofill D on the size of initial data. If so that could be
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook
.getActiveWorksheet();
const dataSize = sheet
.getRange("D:D")
.getUsedRange()
.getRowCount() -1
sheet
.getRange("D:D")
.insert(ExcelScript.InsertShiftDirection.right)
sheet
.getRange("D1:D2")
.setFormulasLocal([["Duration_Minutes"], ["=(E2/60)"]])
sheet
.getRange("D:E")
.setNumberFormatLocal("General")
sheet
.getRange("D2")
.setNumberFormatLocal("0.00")
sheet
.getRange("D2")
.autoFill(
sheet
.getRange("D2")
.getResizedRange(dataSize - 1, 0)
, ExcelScript.AutoFillType.fillDefault)
}
Aug 31 2023 05:51 PM
Sep 01 2023 09:25 AM
If to remove first rows with zero (not blank or empty string) in column D and after that add durations as before, that could be
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook
.getActiveWorksheet();
//here we remove rows with 0 in column D
const range = sheet
.getRange("D:D")
.getUsedRange()
const values = sheet
.getRange("D:D")
.getUsedRange()
.getValues()
let deleteRows: number[] = [];
let k: number = 0;
values.forEach( v => {
if ( v[0] == 0) {
deleteRows.push(k);
}
k++;
})
for (let n = deleteRows.length-1; n > -1; n--) {
range
.getCell(deleteRows[n], 0)
.getEntireRow().delete(ExcelScript.DeleteShiftDirection.up);
}
// next shift columns to the right
// and insert durations
const dataSize = sheet
.getRange("D:D")
.getUsedRange()
.getRowCount() -1
sheet
.getRange("D:D")
.insert(ExcelScript.InsertShiftDirection.right)
sheet
.getRange("D1:D2")
.setFormulasLocal([["Duration_Minutes"], ["=(E2/60)"]])
sheet
.getRange("D:E")
.setNumberFormatLocal("General")
sheet
.getRange("D2")
.setNumberFormatLocal("0.00")
sheet
.getRange("D2")
.autoFill(
sheet
.getRange("D2")
.getResizedRange(dataSize - 1, 0)
, ExcelScript.AutoFillType.fillDefault)
}
Sep 01 2023 11:24 AM
Mar 04 2024 03:07 PM
@SergeiBaklan I'm working on something similar, but I'm not sure what the input is for dataSize in Row 31 of the code. Everything is working fine, but how do I change "L2:L751" to fill down when the number of rows in the column change periodically?
Mar 05 2024 04:29 AM
If "number of rows in the column change periodically" you need to define such range. You may use getUsedRange() before applying other steps or to use other logic. But in any case such logic is to be defined: how do I know how many rows in the column? If have an answer what is the logic is you may apply proper OfficeScript function(s).
Sep 26 2024 05:47 AM
Hello @SergeiBaklan
Sep 26 2024 07:17 AM
That could be like
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getActiveWorksheet()
sheet.getRange("E:H").insert(ExcelScript.InsertShiftDirection.right)
sheet.getRange("E1:H1")
.setValues([["Time taken", "12 Hr Timeline", "12 Hr Status", "AHT"]])
const format = sheet.getRange().getFormat()
format.autofitRows()
format.setRowHeight(21.75)
format.autofitColumns()
const size = sheet.getUsedRange().getRowCount()-2
const fill = (cell: string, formula: string) => {
sheet.getRange(cell).setFormulaLocal(formula)
sheet.getRange(cell).autoFill(
sheet.getRange(cell)
.getResizedRange(size,0), ExcelScript.AutoFillType.fillDefault)
}
fill("E2", "=C2-B2")
fill("F2", "= 0.5")
fill("G2", "=IF(E2 <=F2,\"On Target\",\"Not On Target\")")
fill("H2", "=IF(E2<0.166664,\"1.5\",IF(E2<0.333328,\"1\",IF(E2<0.5,\"0.7\",IF(E2>=0.5,\"0.5\"))))")
}
Sep 26 2024 08:22 AM
Sep 28 2024 06:53 AM
@SergeiBaklan Sorry to disturb you again.
I tried to modify the code to filter column J value and change "In Progress" & "Pending" to "Assigned", and "Closed" to "Resolved" and any column J cell with "Canceled" should be deleted as an entire Row.
The script successfully deletes the entire rows with "Canceled" but doesn't change the other values.
I also got an error that says:
Line 65: Range setValues: The argument is invalid or missing or has an incorrect format.
Please assist.
Below is my code:
Sep 28 2024 12:37 PM - edited Sep 28 2024 02:03 PM
You need first replace values in existing range, after that to modify range deleting some rows
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getActiveWorksheet()
const col: ExcelScript.Range =
workbook
.getActiveWorksheet()
.getUsedRange()
.getColumn(9)
let id: number[] = []
for (let i: number = 0; i < col.getRowCount(); i++) {
const cell: ExcelScript.Range = col.getCell(i,0)
const value = cell.getValue()
value === "In Progress" || value === "Pending" ? cell.setValue("Assigned") :
value === "Closed" ? cell.setValue("Resolved") :
value === "Canceled" ? id.push(i) : ''
}
for (let i = id.length - 1; i > -1; i--) {
col
.getCell(id[i],0)
.getEntireRow()
.delete(ExcelScript.DeleteShiftDirection.up)
}
}
Sep 28 2024 04:40 PM
@SergeiBaklan This is well noted.
Thank you once again, you are a lifesaver.
I sincerely appreciate your effort.
Aug 30 2023 04:00 AM
SolutionIf I understood correctly initially you have some data in column D. You shift it to the right, applied some formulae and formats and autofill D on the size of initial data. If so that could be
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook
.getActiveWorksheet();
const dataSize = sheet
.getRange("D:D")
.getUsedRange()
.getRowCount() -1
sheet
.getRange("D:D")
.insert(ExcelScript.InsertShiftDirection.right)
sheet
.getRange("D1:D2")
.setFormulasLocal([["Duration_Minutes"], ["=(E2/60)"]])
sheet
.getRange("D:E")
.setNumberFormatLocal("General")
sheet
.getRange("D2")
.setNumberFormatLocal("0.00")
sheet
.getRange("D2")
.autoFill(
sheet
.getRange("D2")
.getResizedRange(dataSize - 1, 0)
, ExcelScript.AutoFillType.fillDefault)
}