cancel
Showing results for 
Search instead for 
Did you mean: 

How do I delete a range in excel using variables in SAP iRPA?

suvendub98
Explorer
249

Hi,

I'm trying to delete a range in excel in SAP iRPA using variables as range, but not able to do the same as it comes back with the below error:

'Step GLOBAL.newWorkflow.stDeleteRange: Error, [ctx.excel.sheet.deleteRange] Failed to deleteRange in excel workbook.Expected ';''

Below is my code. Any help is appreciated. Thanks.

// ----------------------------------------------------------------
//   Step: stDeleteRange
// ----------------------------------------------------------------
GLOBAL.step({ stDeleteRange: function(ev, sc, st) {
	var rootData = sc.data;
	ctx.workflow('newWorkflow', 'e5fa96cc-a744-43de-898d-a989a30c5263') ;
	
	var ExcelFile = "C:\\Users\\suvendub\\Test_DeleteRange.xlsx";
	if (ctx.fso.file.exist(ExcelFile))
	{
		ctx.excel.file.open(ExcelFile);
		ctx.log("File Opened:: " +ExcelFile);
		var oObject = ctx.excel.file.open(ExcelFile, false);
		var BookList = null;
		var retryCount = 0;
		while ((BookList == null) && (retryCount < 3)) 
		{
			try
			{
				retryCount++;
				BookList = ctx.excel.sheet.getList();
				ctx.log("BookList:: " +BookList);
			}
			catch (error)
			{
				ctx.log("Unable to read excel. Retrying after 2 seconds... " + retryCount);
				ctx.sleep(2000);
			}
		}
		var Sheet1 = BookList[0];
		ctx.excel.sheet.activate(Sheet1);

		var index = 2;
	
		// Deleting the records exceeding the number of days specified
		while((ctx.excel.sheet.getCell(index, 1)) != null) 
		{
			ctx.log("Data:: " +ctx.excel.sheet.getCell(index, 8));
			if(ctx.excel.sheet.getCell(index, 😎 > 5)
			{
				ctx.log("index: " +index); // Returns 4
				var cellEnd = ctx.excel.sheet.getCell(index, 5);
				cellEnd = Number(cellEnd) + Number(index);
				ctx.log("cellEnd: " +cellEnd); //Returns 6
				//delete the record
				var str = "'" +Number(index) +":" +cellEnd +"'";
				ctx.log("str: " +str); // Returns '4:6'
				ctx.excel.sheet.deleteRange(str); // This line produces ERROR
				index++
			}
			else
			{
				index++;
			}
		}
		ctx.excel.file.save(oObject);
		ctx.excel.file.close(oObject, true);
		ctx.excel.end();
	}
	sc.endStep(); // end Scenario
	return;
}});
View Entire Topic
former_member717738
Participant

Hi Suvendu,

There is no need of single inverted commas while formatting str. Following is sufficient.

var str = Number(index) + ":" + cellEnd ;

Br,

Thousif.

suvendub98
Explorer
0 Kudos

Hi Thousif,

You just saved my day! Thank you so much!