I have a few words/ phrases i need deleted from a google sheet quite regularly.
The code will only do 1 word and I would like it to do as many as I need.
function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Log');
var r = s.getRange('A:Z');
var v = r.getValues();
for(var i=v.length-1;i>=0;i--)
if(v[0,i]=='Tom')
s.deleteRow(i+1);
};
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);
I hope my Intention is clear. It's for inventory management. The first cell Shows the current inventory and the second one is just a cell where you can type in your entries or outflow. I think the Code for Excel should be something like this, but I'm looking for the corresponding Google spreadsheet:
Sub LagerNeu()
Dim S As Integer
[F2].Select
S = ActiveCell.Value + ActiveCell.Offset(0, 1).Value
ActiveCell.Value = S
ActiveCell.Offset(0, 1).Value = 0
End Sub
Using the onEdit() method can watch for changes to the F column and update the cells as needed
Example:
function onEdit(e) {
var range = e.range; // e.g F2 is edited
// Only runs if the edit is in column F
if (range.getColumn() == 6) {
var old_value = range.offset(0, -1).getValue();
// Set the new value to the cell in the column to the left - E2
range.offset(0, -1).setValue(Number(old_value) + Number(e.value));
// Reset F2 to zero
range.setValue(0);
}
}
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();
}
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.