Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88050

Deleting an Excel row after processing in PowerShell

$
0
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
}

Viewing all articles
Browse latest Browse all 88050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>