How can I add formatting within a cell that only applies to part of the cell value using EPPlus? - epplus

I know how to format cell values in EPPlus but only if the format applies to the whole value. How can I add formatting that only applies to part of the cell value?
for example...

Use the RichText collection to build it:
using (var pck = new ExcelPackage())
{
var wb = pck.Workbook;
var ws = wb.Worksheets.First();
var cell = ws.Cells["B2"];
cell.RichText.Add("This is some ");
cell.RichText.Add("formatting");
cell.RichText.Add(" withing a value");
cell.RichText[1].Color = Color.Red;
cell.RichText[1].Size = 14;
pck.Save();
}

Related

Google sheets script - how to concate variable?

I have this code:
function _1() {
var aa = SpreadsheetApp.getActiveSpreadsheet();
var sheet = aa.getSheets()[0];
var cell = sheet.getRange("k1");
var cell = sheet.getvalue("a" + cell + ")");
};
In cell k1 there is a number (variable). I want to use this cell in my function, but its not work for me.
You entire code is correct, except last line:
var cell = sheet.getvalue("a" + cell + ")")
You don't need to import range value again in your getValue and it will cause error, and below is the correct execution by convert value to string (*remove ; after } ):
function test_1() {
var aa = SpreadsheetApp.getActiveSpreadsheet();
var sheet = aa.getSheets()[0];
var cell = sheet.getRange("B2").getValue();
cell = cell.toString() + "a"
Logger.log(cell);
}
Answer
getValue() does not take any input parameter. Join the strings later
Solution
I attach you a function that reads the number in B2 (X) and writes a string in AX.
function test_1() {
var aa = SpreadsheetApp.getActiveSpreadsheet();
var sheet = aa.getSheets()[0];
var B2 = sheet.getRange("B2").getValue();
var cell = "A" + B2.toString()
sheet.getRange(cell).setValue('test')
}
References
Range.getValue()
Range.setValue(value)

Apps script: Paste filtered range to the next empty row in specific column

I would like to create a code which copies a filtered column and pastes the whole column to the next available row in another sheet. The code below works however pastes the range in a loop until the end of the spreadsheet. How can I paste it just once?
I could not find an answer myself.
I would much appreciate your help!
function CopyFilter() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("MASTER");
var values = sheet.getDataRange().getValues();
Logger.log("VALUES "+values.length);
var criteria = SpreadsheetApp.newFilterCriteria()
.whenNumberGreaterThanOrEqualTo(1)
.build();
var range = sheet.getFilter()
.setColumnFilterCriteria(52, criteria)
.getRange();
range.getValues();
var sheet2 = ss.getSheetByName("DL CSV OUTPUT");
sheet.getRange("AZ4:AZ").copyTo(sheet2.getRange(2,41,sheet2.getLastRow()+1), {contentsOnly:true});
sheet.getFilter().remove();
}
Do not use the copy method. Simply select the range you are interested in, then get a range (of same size - very important) in the target sheet and set all values at once.
EXAMPLE:
const valuesToCopy = sheet.getRange("AZ4:AZ").getValues();
sheet2.getRange(2,3,valuesToCopy.length).setValues(valuesToCopy);
And no need to worry about: {contentsOnly:true}
Since the above method does not bring over any formatting.
Reference:
setValues(values)

EPPlus how to write actual 'true' value

Using EPPlus, how can I write the actual excel TRUE value to a cell? I'm just getting the text value, and I have to explicitly click into the cell and press enter to have it recognize it as a boolean.
Cell.Value is an object so if you set it as a bool it will box it and preserve the type:
var cells = worksheet.Cells;
cells["A1"].Value = false; // not = "false"
cells["A2"].Value = true; // not = "true"
Which will give you this:

In Google sheets, using a script, can I replace all the functions in cells with the plain text or number values they've calculated?

I have a spreadsheet with multiple sheets. The first sheet is an order form where you can enter customer details and their order summary.
On another sheet, these data are copied to relevant cells in a picking slip and a tax invoice, just using preformatted cells, and simple functions, ie:
='Order Submission Form'!E9
And then that cell is populated with the Customers name.
However, I then have a script which duplicates that spreadsheet, deletes all except the sheet with the picking slip and tax invoice, then turns it into a PDF. All of that code works fine, except that deleting all the redundant sheets also breaks the cell references, and so I end up with the picking slip and tax invoice just populated with #REF! in all the cells.
This is the complete code that I'm using to generate the PDF. I know I copied at least some of it from somewhere else, but I don't remember where I originally got it.
There is a section which seems to suggest it replaces the cell values with text to avoid broken references, but it does not seem to work, and it gives an error: "Incorrect Range height: Was 59 but should be 999 (Line 22, file createPDF)"
function checkSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = "Picking Slips";
var folderID = "1gp3dqdwMSXzwQ6gzhctbQk5ODnpBOHB_"; // Folder id to save in a folder.
var orderNumber = ss.getRange("'Picking Slips'!G4").getValue()
var pdfName = "Picking Slip # "+ orderNumber + " - " + Utilities.formatDate(new Date(), "GMT+8", "yyyy-MM-dd");
var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
var folder = DriveApp.getFolderById(folderID);
//Copy whole spreadsheet
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder))
//Dump the contents of the picking slip into order history
var destSheet = destSpreadsheet.getSheets()[0];
//repace cell values with text (to avoid broken references)
var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns());
var sourcevalues = sourceRange.getValues();
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
destRange.setValues(sourcevalues);
//delete redundant sheets
var sheets = destSpreadsheet.getSheets();
for (i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName){
destSpreadsheet.deleteSheet(sheets[i]);
}
}
//save to pdf
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var url = 'https://docs.google.com/spreadsheets/d/'+sourceSpreadsheet.getId()+'/export?exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
+ '&size=A4' // paper size legal / letter / A4
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true' // fit to page width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&gid='+sourceSheet.getSheetId(); // the sheet's Id
var newFile = folder.createFile(theBlob);
//Delete the temporary sheet
DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);
}
Any help would be greatly appreciated.
If you select the whole sheet and select copy and then paste special > paste values, only you will overwrite every formula in the sheet with its value.
You can script this as follows:
var range = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
range.copyTo(range, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);

Google Sheets Addon or Formula that accomplishes the same thing as the Prevsheet Macro's for Excel

I want to have a cumulative total running on each sheet (so the current total will be a value on the current sheet summed with the cumulative total from the previous sheet, each sheet representing a day of the month. In Excel this was definable in a Macro similar to this.
Function PrevSheet(rg As Range)
n = Application.Caller.Parent.Index
If n = 1 Then
PrevSheet = CVErr(xlErrRef)
ElseIf TypeName(Sheets(n - 1)) = "Chart" Then
PrevSheet = CVErr(xlErrNA)
Else
PrevSheet = Sheets(n - 1).Range(rg.Address).Value
End If
End Function
Is there anyway to do this in Google Sheets. It keeps referring back to sheet 1. Thanks
I left an editable link here which is a copy of what I am working on.
https://docs.google.com/spreadsheets/d/1cx9w9vOJXKj2WqYBoJOuZ7dWqP1VunFYpZbMLjdpkKQ/edit?usp=sharing
You can do that only using a script. Try this:
function PrevSheet(range) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var active = ss.getActiveSheet().getName();
for (i in sheets) {
if (sheets[i].getName() === active) {
return (sheets[i-1].getRange(range).getValues());
}
}
}
The only problem is, you will have to push the range as a String by surrounding it with quotation marks.
This will give you a range of Data
=PrevSheet("D2:D6")
This will give you the sum
=Sum(PrevSheet("D2:D6"))
UPDATE:
If you want this to be updated, use this script
function PrevSheetName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var active = ss.getActiveSheet().getName();
for (i in sheets) {
if (sheets[i].getName() === active) {
return (sheets[i-1].getName());
}
}
}
And get the values like this:
=INDIRECT(PrevSheetName()&"!D2")
And
=SUM(INDIRECT(PrevSheetName()&"!D2:D6"))
Hope this helps.