google apps script addition to variable - variables

Why when I run this function, "1" is appended to the variable, instead of added to it.
I guess my syntax is wrong , but couldent figure out why? :\
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName("TEST");
//get last cell with data on cloumn A
var columnA = dataSheet.getRange("A" + dataSheet.getMaxRows());
var Alast = columnA.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1);
//get last cell with data on cloumn C
var columnC = dataSheet.getRange("C" + dataSheet.getMaxRows());
var Clast = columnC.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1);
for (var counter = Clast + 1; counter <= Alast; counter = counter +1) {
Logger.log(counter);}
}
Here is the logger output:
1
11
111
Execution cancelled
Thanks

I thought that columnA.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1) and columnC.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1) return the string value. I thought that this is the reason of your issue. In your script, how about the following modification?
From:
//get last cell with data on cloumn A
var columnA = dataSheet.getRange("A" + dataSheet.getMaxRows());
var Alast = columnA.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1);
//get last cell with data on cloumn C
var columnC = dataSheet.getRange("C" + dataSheet.getMaxRows());
var Clast = columnC.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1);
To:
//get last cell with data on cloumn A
var columnA = dataSheet.getRange("A" + dataSheet.getMaxRows());
var Alast = columnA.getNextDataCell(SpreadsheetApp.Direction.UP).getRow(); // Modified
//get last cell with data on cloumn C
var columnC = dataSheet.getRange("C" + dataSheet.getMaxRows());
var Clast = columnC.getNextDataCell(SpreadsheetApp.Direction.UP).getRow(); // Modified
By this modification, Alast and Clast are the values of integer.
Reference:
getRow()

Related

Force single line of string in VObject

I am trying to create vCards (Email contacts) unsing the vobject library and pandas for python.
When serializing the values I get new lines in the "notes" of my output(no new lines in source). In every new line, created by ".serialize()", there is also a space in the beginning. I would need to get rid of both.
Example of output:
BEGIN:VCARD
VERSION:3.0
EMAIL;TYPE=INTERNET:test#example.at
FN:Valentina test
N:Valentina;test;;;
NOTE:PelletiererIn Mitglieder 2 Preiserhebung Aussendung 2 Pressespiegelver
sand 2 GeschäftsführerIn PPA_PelletiererInnen GeschäftsführerIn_Pellet
iererIn
ORG:Test Company
TEL;TYPE=CELL:
TEL;TYPE=CELL:
TEL;TYPE=CELL:
END:VCARD
Is there a way that I can force the output in a single line?
output = ""
for _,row in df.iterrows():
j = vobject.vCard()
j.add('n')
j.n.value = vobject.vcard.Name(row["First Name"],row["Last Name"])
j.add('fn')
j.fn.value = (str(row["First Name"]) + " " + row["Last Name"])
o = j.add("email")
o.value = str((row["E-mail Address"]))
o.type_param = "INTERNET"
#o = j.add("email")
#o.value = str((row["E-mail 2 Address"]))
#o.type_param = "INTERNET"
j.add('org')
j.org.value = [row["Organization"]]
k = j.add("tel")
k.value = str(row["Home Phone"])
k.type_param = "CELL"
k = j.add("tel")
k.value = str(row["Business Phone"])
k.type_param = "CELL"
k = j.add("tel")
k.value = str(row["Mobile Phone"])
k.type_param = "CELL"
j.add("note")
j.note.value = row["Notiz für Kontaktexport"]
output += j.serialize()
print(output)

How to get the value from a table cell

I have a table I created and am most interested in the "costs" column. I could have more than one row of data in which I would have multiple costs and I want to add those up and put the result in the table cell next to "Total." I saw a method called GetValue(); but not sure if that is what I am looking for or how to use it. My thinking is that migradoc has a method where you can get the value of a table cell in which I would store that in a variable. And where I create the "Total" row, I would use that variable to display the total. So how would I do that?
My code:
/define header of table
Row row = table.AddRow();
row.HeadingFormat = true;
Cell cell = row.Cells[0];
cell.AddParagraph("Customer Name");
cell.Format.Font.Bold = true;
cell = row.Cells[1];
cell.AddParagraph("Date Created");
cell.Format.Font.Bold = true;
cell = row.Cells[2];
cell.AddParagraph("Description");
cell.Format.Font.Bold = true;
cell = row.Cells[3];
cell.AddParagraph("Due Date");
cell.Format.Font.Bold = true;
cell = row.Cells[4];
cell.AddParagraph("Billable Hours");
cell.Format.Font.Bold = true;
cell = row.Cells[5];
cell.AddParagraph("Costs");
cell.Format.Font.Bold = true;
//define a row of data in the table
foreach (TicketView1 ticket in SampleTickets)
{
row = table.AddRow();
cell = row.Cells[0];
cell.AddParagraph(ticket.customer_name);
cell = row.Cells[1];
cell.AddParagraph(ticket.date_created.ToString("MM/dd/yyyy"));
cell = row.Cells[2];
cell.AddParagraph(ticket.description);
cell = row.Cells[3];
cell.AddParagraph(ticket.due_date.ToString("MM/dd/yyyy"));
cell = row.Cells[4];
cell.AddParagraph(ticket.billable_hrs.ToString());
}
cell = row.Cells[5];
cell.AddParagraph("$60.00");
//add invisible row as a space line to the table
row = table.AddRow();
row.Borders.Visible = false;
//add the subtotal row
row = table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("Sub Total:");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
cell = row.Cells[5];
//add tax row
row = table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("TAX:");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
//add total
row = table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("TOTAL:");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
}
I have found out the answer by simple creating a count variable and then inside my forEach loop just adding that sum plus my dummy data which would be 60. Then right where I created my "totals" row, right underneath it I added into the cell that variable which contains the count data.
My forEach loop and count variable
double sum = 0;
foreach (TicketView1 ticket in SampleTickets)
{
row = table.AddRow();
cell = row.Cells[0];
cell.AddParagraph(ticket.customer_name);
cell = row.Cells[1];
cell.AddParagraph(ticket.date_created.ToString("MM/dd/yyyy"));
cell = row.Cells[2];
cell.AddParagraph(ticket.description);
cell = row.Cells[3];
cell.AddParagraph(ticket.due_date.ToString("MM/dd/yyyy"));
cell = row.Cells[4];
cell.AddParagraph(ticket.billable_hrs.ToString());
cell = row.Cells[5];
cell.AddParagraph("$60.00");
sum = sum + 60;
}
Where I added the total row and appended the data to the totals cell:
//add total
row = table.AddRow();
row.Cells[0].Borders.Visible = false;
row.Cells[0].AddParagraph("TOTAL:");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
row.Cells[5].AddParagraph(sum.ToString("$0.00"));

How to copy values that have just been deleted into 'Archive' Sheet?

Setting up so any expired date rows get deleted. But I would Like to Delete and Move to Archive tab(sheet).
Have tried taking other codes and integrating but cannot find the code.
function DeleteOldEntries() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Current");//assumes Current is the name of the sheet
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
var currentDate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);//tomorrow
for (i=lastrow;i>=2;i--) {
var tempDate = values[i-1][1];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if ((tempDate!=NaN) && (tempDate <= currentDate))
{
sheet.deleteRow(i);
}//closes if
}//closes for loop
}//closes function

c# EPPlus: how to fix - ShowFilter doesn't work

The issue with Table.ShowFilter, using EPPLUS library.
Created new ExcelTable in the sheet, but can't apply Table.ShowFilter = false.
The filters are still in the table.
TableStyle and StyleName work fine.
ExcelTable et = (ExcelTable)Table;
int firstRow = newRow;
int lastRow;
if (DataStructure.Data != null)
lastRow = newRow + DataStructure.Data.Count();
else
lastRow = newRow + 1;
int firstColumn = OriginalAddress.StartColumn;
int lastColumn = OriginalAddress.EndColumn;
ExcelRange rg = ws.Cells[firstRow, firstColumn, lastRow, lastColumn];
Guid guid = Guid.NewGuid();
string str_guid = guid.ToString();
string tableName = et.Name + "_" + str_guid;
ExcelTable tab = wsTarget.Tables.Add(rg, tableName);
// tab.ShowHeader = et.ShowHeader;
// tab.TableStyle = et.TableStyle;
// tab.StyleName = et.StyleName;
tab.ShowFilter = false;
Right table is desired result.
Please, help to fix!
Is this you want to mean?
tab.ShowHeader = false;
This very little example works here :
using (ExcelPackage xls = new ExcelPackage())
{
ExcelWorksheet ws2 = xls.Workbook.Worksheets.Add("f1");
OfficeOpenXml.Table.ExcelTable tab = ws2.Tables.Add(new ExcelAddressBase("a1:e5"), "table");
//tab.TableStyle = et.TableStyle;
//tab.StyleName = et.StyleName;
tab.ShowFilter = false;
FileInfo f = new FileInfo(#"d:\temp\test.xlsx");
xls.SaveAs(f);
}
Probably, the bug.
Adding ws2.DeleteColumn(1, 1) to the code, add filters to the table, even with tab.ShowFilter = false.
Workaround: put tab.ShowFilter = false right before file saving.
using (ExcelPackage xls = new ExcelPackage())
{
ExcelWorksheet ws2 = xls.Workbook.Worksheets.Add("f1");
OfficeOpenXml.Table.ExcelTable tab = ws2.Tables.Add(new ExcelAddressBase("d3:g8"), "table");
//tab.TableStyle = et.TableStyle;
//tab.StyleName = et.StyleName;
ws2.DeleteColumn(1, 1);
tab.ShowFilter = false;
FileInfo f = new FileInfo(#"test.xlsx");
xls.SaveAs(f);
}

EPPlus Pivot Table - Copy Values to New Sheet

I've successfully created a pivot table using EPPlus. The pivot table resides in a seperate sheet from the raw data. I would like to copy the pivot table data to a new, third, sheet, but just the values, not the pivot defintions. Does EPPlus support this?
You can just copy the datasource range via Cache Definition:
public void PivotDataCopy()
{
const string FORMATCURRENCY = "#,###;[Red](#,###)";
var file = new FileInfo(#"c:\temp\temp.xlsx");
if (file.Exists)
file.Delete();
var pck = new ExcelPackage(file);
var workbook = pck.Workbook;
var dataworksheet = workbook.Worksheets.Add("datasheet");
//The data
dataworksheet.Cells["A20"].Value = "Col1";
dataworksheet.Cells["A21"].Value = "sdf";
dataworksheet.Cells["A22"].Value = "wer";
dataworksheet.Cells["A23"].Value = "ghgh";
dataworksheet.Cells["A24"].Value = "sdf";
dataworksheet.Cells["A25"].Value = "wer";
dataworksheet.Cells["B20"].Value = "Col2";
dataworksheet.Cells["B21"].Value = "Group A";
dataworksheet.Cells["B22"].Value = "Group B";
dataworksheet.Cells["B23"].Value = "Group A";
dataworksheet.Cells["B24"].Value = "Group C";
dataworksheet.Cells["B25"].Value = "Group A";
dataworksheet.Cells["C20"].Value = "Col3";
dataworksheet.Cells["C21"].Value = 453.5;
dataworksheet.Cells["C22"].Value = 634.5;
dataworksheet.Cells["C23"].Value = 274.5;
dataworksheet.Cells["C24"].Value = 453.5;
dataworksheet.Cells["C25"].Value = 634.5;
dataworksheet.Cells["D20"].Value = "Col4";
dataworksheet.Cells["D21"].Value = 686468;
dataworksheet.Cells["D22"].Value = 996440;
dataworksheet.Cells["D23"].Value = 185780;
dataworksheet.Cells["D24"].Value = 686468;
dataworksheet.Cells["D25"].Value = 996440;
//The pivot table
var pivotworksheet = workbook.Worksheets.Add("pivotsheet");
var pivotTable = pivotworksheet.PivotTables.Add(pivotworksheet.Cells["A1"], dataworksheet.Cells["A20:D29"], "test");
//The label row field
pivotTable.RowFields.Add(pivotTable.Fields["Col1"]);
pivotTable.DataOnRows = false;
//The data fields
var field = pivotTable.DataFields.Add(pivotTable.Fields["Col3"]);
field.Name = "Sum of Col2";
field.Function = DataFieldFunctions.Sum;
field.Format = FORMATCURRENCY;
field = pivotTable.DataFields.Add(pivotTable.Fields["Col4"]);
field.Name = "Sum of Col3";
field.Function = DataFieldFunctions.Sum;
field.Format = FORMATCURRENCY;
//Get the pivot table data source
var newworksheet = workbook.Worksheets.Add("newworksheet");
var pivotdatasourcerange = pivotTable.CacheDefinition.SourceRange;
pivotdatasourcerange.Copy(newworksheet.Cells["A1"]);
pck.Save();
}
EDIT: Doing with a VBA macro which then resaves the sheet as a non-macro XLSX:
public void PivotDataCopy()
{
const string FORMATCURRENCY = "#,###;[Red](#,###)";
var file = new FileInfo(#"c:\temp\temp.xlsm");
if (file.Exists)
file.Delete();
var pck = new ExcelPackage(file);
var workbook = pck.Workbook;
var dataworksheet = workbook.Worksheets.Add("datasheet");
//The data
dataworksheet.Cells["A20"].Value = "Col1";
dataworksheet.Cells["A21"].Value = "sdf";
dataworksheet.Cells["A22"].Value = "wer";
dataworksheet.Cells["A23"].Value = "ghgh";
dataworksheet.Cells["A24"].Value = "sdf";
dataworksheet.Cells["A25"].Value = "wer";
dataworksheet.Cells["B20"].Value = "Col2";
dataworksheet.Cells["B21"].Value = "Group A";
dataworksheet.Cells["B22"].Value = "Group B";
dataworksheet.Cells["B23"].Value = "Group A";
dataworksheet.Cells["B24"].Value = "Group C";
dataworksheet.Cells["B25"].Value = "Group A";
dataworksheet.Cells["C20"].Value = "Col3";
dataworksheet.Cells["C21"].Value = 453.5;
dataworksheet.Cells["C22"].Value = 634.5;
dataworksheet.Cells["C23"].Value = 274.5;
dataworksheet.Cells["C24"].Value = 453.5;
dataworksheet.Cells["C25"].Value = 634.5;
dataworksheet.Cells["D20"].Value = "Col4";
dataworksheet.Cells["D21"].Value = 686468;
dataworksheet.Cells["D22"].Value = 996440;
dataworksheet.Cells["D23"].Value = 185780;
dataworksheet.Cells["D24"].Value = 686468;
dataworksheet.Cells["D25"].Value = 996440;
//The pivot table
var pivotworksheet = workbook.Worksheets.Add("pivotsheet");
var pivotTable = pivotworksheet.PivotTables.Add(pivotworksheet.Cells["A1"], dataworksheet.Cells["A20:D29"], "test");
//The label row field
pivotTable.RowFields.Add(pivotTable.Fields["Col1"]);
pivotTable.DataOnRows = false;
//The data fields
var field = pivotTable.DataFields.Add(pivotTable.Fields["Col3"]);
field.Name = "Sum of Col2";
field.Function = DataFieldFunctions.Sum;
field.Format = FORMATCURRENCY;
field = pivotTable.DataFields.Add(pivotTable.Fields["Col4"]);
field.Name = "Sum of Col3";
field.Function = DataFieldFunctions.Sum;
field.Format = FORMATCURRENCY;
//add the macro to copy the table data on open
workbook.Worksheets.Add("newworksheet");
var sb = new StringBuilder();
sb.AppendLine("Private Sub Workbook_SheetCalculate(ByVal Sh As Object)");
sb.AppendLine(" Sheets(\"pivotsheet\").Cells.Copy");
sb.AppendLine(" Sheets(\"newworksheet\").Range(\"A1\").PasteSpecial Paste:=xlPasteValues");
sb.AppendLine(" Selection.Clear");
sb.AppendLine(" Application.DisplayAlerts = False");
sb.AppendLine(String.Format(" ActiveWorkbook.SaveAs \"{0}\", xlOpenXMLWorkbook", file.FullName.Replace("xlsm", "xlsx")));
sb.AppendLine(" Application.DisplayAlerts = True");
sb.AppendLine("End Sub");
pck.Workbook.CreateVBAProject();
pck.Workbook.CodeModule.Code = sb.ToString();
pck.Save();
}