0

I am trying to delete the 2nd row after the loop is complete.

The idea is that if something goes wrong whilst the script is running, I want to be able to delete the rows it has already processed and read. this means that when I re-run the script, it won't restart from the beginning of the script.

i tried implementing this Deleting an entire row in Excel using PowerShell , but was having issues trying to make it work.

I have also tried, Delete first four rows in Excel with Powershell

but when I reopen excel, all the rows are still there and if I close PowerShell mid running, the rows are also still there.

My Code:

#locate script root directory
$rootDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

#load helper functions
. ($rootDirectory + ".\Helpers-Excel.ps1")
. ($rootDirectory + ".\Helpers-Type.ps1")

#define Type template to be activated and Excel data to be loaded
$templatePath = $rootDirectory + ".\Vikie_Test_Pavers.cintitle"
$excelDataPath = $rootDirectory + ".\PaversProductInfo.xlsx"

# import data from Excel file
$newsData = Excel-ImportData $excelDataPath

# restart trigger - unique value to cause scene restart
$newsNumber = 1

#ensure template is visible
Type-ActivateTemplate $templatePath

# iterate through each news line and post updates to the variables
foreach($news in $newsData)
{
    #ensure restart trigger is acrtivated by passing new value - news number
    $newsNumber += 1
    #ensure value is cueued by avoiding duplicates as same value is ignored by Type
    if($newsNumber % 1 -eq 0) {$appender = " "} else {$appender=""}

    #push updates to Category and News variables
    Type-UpdateVariable 'ProductNumber' ($news.ProductNumber + $appender)
    Type-UpdateVariable 'ProductDescription' ($news.ProductDescription + $appender)
    Type-UpdateVariable 'ProductFit' ($news.ProductFit + $appender)
    Type-UpdateVariable 'ProductPrice' ($news.ProductPrice + $appender)
    Type-UpdateVariable 'ProductPostage' ($news.ProductPostage + $appender)
    Type-UpdateVariable 'ProductColour' ($news.ProductColour + $appender)
    Type-UpdateVariable 'ProductSize' ($news.ProductSize + $appender)
    Type-UpdateVariable 'Ticker' ($news.Ticker + $appender)
    Type-UpdateVariable 'PhoneNumber' ($news.PhoneNumber + $appender)
    Type-UpdateVariable 'TsandCs' ($news.TsandCs + $appender)
    Type-UpdateVariable 'RestartTrigger' $newsNumber

    #make sure values are applied by letting some time before next update
    #this will wait 180, then move to the next line on the product list  "Abul"
    Start-Sleep -seconds $news.Duration
}
6
  • What module are you using that has functions like Excel-ImportData and Type-UpdateVariable ? I don't see you removing anything from the excel file or saving the updated file?? Commented Jan 6, 2020 at 12:28
  • I had to remove those commands I did because I believed it was wrong. I need it to delete when it's doing the loop Commented Jan 6, 2020 at 13:41
  • This is the full code pastebin.com/RtisgmiB Commented Jan 6, 2020 at 13:41
  • 1
    Looking at the code, I think it may be easiest if you [1] Keep track of the line number you are updating from [2] Save that to a log file maybe or in a registry value, so next time you run it, you can read the line number back and start iterating from there. That way you won't have to do anything to the Excel file as such. Just before entering the loop, get the last linenumber in a variable and instead of foreach($news in $newsData), use a counter loop for ($i = $lastline; $i -lt $newsData.Count; $i++) { $news = $newsData[$i]; #etcetera } Commented Jan 7, 2020 at 14:52
  • 1
    P.S. In your function Excel-ImportData may I suggest you end it using Import-Csv -path $csvFile -UseCulture. That last switch will ensure your code would also run on a system with a different locale. Commented Jan 7, 2020 at 14:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.