I am still learning javascript so excuse me for probably a stupid question.
I searched for the title of my question and the closest I found was this link:
Google Sheets copy and paste row into another sheet and then delete content from the first sheet
What I need to do is to search for the word "Kicked" in this google sheet:
https://docs.google.com/spreadsheets/d/1UM3sZ3AK6_-khzfNgyyYesn56DrkKS2zSV20zorzpPg/edit?usp=sharing
from columns F to AJ in rows 2 to 101 so my search range is F2:AJ101.
the current code I use in the script is as follows:
function onEdit(e) {
var sheet=e.range.getSheet();
if (sheet.getSheetName() == 'Test') {
logSheet = e.source.getSheetByName('Kicked');
var row = e.range.getRow();
var lastRow = logSheet.getLastRow();
var range = sheet.getRange(row, 1, 1, sheet.getLastColumn());
range.copyTo(logSheet.getRange(lastRow+1,1),{contentsOnly:true});
sheet.clearContent();
}
}
}
However, the code does not empty the row once it is copied to the new sheet.
If anyone can help me, I would really appreciate it.
Related
First of all, thank you for all your participation, most of my answers are already replied in this stack overflow.
So, I would like to have, in a specific column, for each new form submitted, a specific number (like a tracking number for each form).
I did this code, which is working perfectly to do what i want :
function onFormSubmit(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var row = SpreadsheetApp.getActiveSheet().getLastRow();
sheet.getRange(row,1).setValue(row+10071);
}
But my issue,that is working in all the sheet of my spreadsheet, and I want it only in a specific sheet. So I tried with getSheetByName like this :
function onFormSubmit(e) {
var sheet = SpreadsheetApp.getSheetByName(mysheetname);
var mysheetname="Module2"
var row = SpreadsheetApp.getSheetByName().getLastRow();
sheet.getRange(row,1).setValue(row+10071);
}
But is not working at all.
Sorry for my bad english, but I did my best to be clear.
If someone can help me, I will be very grateful.
Regards
Paul
Perhaps try adding an IF to restrict the numbering to only that sheet: if (sheet.getName() = "*your sheet name*")
So, all together it would be:
function onFormSubmit(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var row = SpreadsheetApp.getActiveSheet().getLastRow();
if (sheet.getName() = "*your sheet name*") {
sheet.getRange(row,1).setValue(row+10071);
}
}
I am currently trying to write a script on Google apps script for Google Sheets that will insert a row directly underneath the current active cell. I also want to copy down the contents of only Col B.
Here is what I have thus far:
This snip-bit works great, but there is no "active cell" feature
function addrow5() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1];
// This inserts a row after the first row position
sheet.insertRowAfter(5);
So, I've researched and found found this:
sheet.insertRowAfter(activeCell.getRow());
but I'm not exactly how to put it all together. As far as the copying of Col B, I've yet to chart those waters.
Thank you for any help!
I probably answered this one already on the google docs Help forum but in case somenone else is interested:
function addrow() {
var sheet = SpreadsheetApp.getActiveSheet(),
r = sheet.getActiveCell()
.getRow(),
bVal = sheet.getRange(r, 2).getValue();
sheet.insertRowAfter(r);
sheet.getRange(r + 1, 2).setValue(bVal);
}
I've been struggling trying to find a method to place information from a cell (a range of cells) as comments in a different cell (range of cell) in a different sheet.
Something like this, if I have Apple in Cell A1 in Sheet1 I want Apple to be inserted as a comment in Cell F1 in Sheet 2.
I tried coming up with something like this.
//I'm still working on this I have not been able to make this work//
//Ideally this will put the phone numbers as comment's in the needed cases//
var ss = SpreadsheetApp.getActiveSpreadsheet();
var targetsheet = ss.getSheetByName("report");
var sourcesheet = ss.getSheetByName("Sheet1");
var nrange = sourcesheet.getRange(2, 3, sourcesheet.getLastRow(), 1)
var sourcenotes = [nrange.getValue()]
var notes = targetsheet.getRange(2, 6, sourcesheet.getLastRow(),1)
notes.setNotes(sourcenotes);
As you can read this is not working I've tried different methods but none is working so I come to you guys for help.
The function "setNotes" takes a 2 dimension array, the value should be like this setNotes([sourcenotes]). Check the documentation.
Also if you are going to set the note for just one cell, i would recomend to use the function setNote. Here is an example on that function.
I've been busy trying to use the build-in javascript in Google Spreadsheet, however, not having worked in either javascript or Google Spreadsheet, i'm having a few difficulties.
My script is supposed to read a number (1-3) in a cell, and from that number parse an image to the cell below (I've been using the setFormula command for this).
So far it's working for 1 cell (B6 as i've choosen right now), but i would like to loop through a column with numbers in every other cell (So that after the script has run, it's number-picture-number-picture etc) - i just can't figure out how.
The code i'm using right now:
function numbtoimage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var url = 'IMAGE("https://dl.dropboxusercontent.com/s/bpqy8o796casqjl/belt.JPG?dl=0", 2)';
var url2 = 'IMAGE("https://dl.dropboxusercontent.com/s/4q8sakhkpot0h65/belt2.JPG?dl=0",2)';
var url3 = 'IMAGE("https://dl.dropboxusercontent.com/s/kvsf4z6z45rcg53/belt3.JPG?dl=0",2)';
var cell = sheet.getRange("B6")
var data = cell.getValue()
if(data==1) {cell.offset(1, 0, 1).setFormula(url);}
else if(data==2) {cell.offset(1, 0, 1).setFormula(url2);}
else if(data==3) {cell.offset(1, 0, 1).setFormula(url3);}
}
I've looked at This similar problem, but have been unable to make it work for my case.
Any help is greatly and truly appreciated!
Nicklas
You need some sort of loop to go through the data. I Would suggest a FOR loop.
Your script is currently written to get one single cell value, rather than all the values.
So it might be an idea to get all values in one go, then check whats in them.
Also from you question, it's not clear where the numbers will be found.
Only in column B?
Here is a quick example (untested), that goes through column B looking for a number and it should insert the link in the cell below based on that number. This code is based on your original example and untested but hopefully it helps.
function numbtoimage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var url = 'IMAGE("https://dl.dropboxusercontent.com/s/bpqy8o796casqjl/belt.JPG?dl=0", 2)';
var url2 = 'IMAGE("https://dl.dropboxusercontent.com/s/4q8sakhkpot0h65/belt2.JPG?dl=0",2)';
var url3 = 'IMAGE("https://dl.dropboxusercontent.com/s/kvsf4z6z45rcg53/belt3.JPG?dl=0",2)';
var values = sheet.getValues();
for(i=0; i < values.lenth ; i++){
if(values[i][1]==1) {sheet.getRange(i+2, 2).setFormula(url);}
else if(values[i][1]==2) {sheet.getRange(i+2, 2).setFormula(url2);}
else if(values[i][1]==3) {sheet.getRange(i+2, 2).setFormula(url3);}
}
}
First off, here are two basic spreadsheets for what I am talking about:
Page one: https://docs.google.com/spreadsheets/d/1GZs_I9_beIO9xYBXatoxStRMmk9Aj3-UxPQpB9-iA-I/edit?usp=sharing
Page two: https://docs.google.com/spreadsheets/d/1R86_0SrAtyIJPAhMW0Ura-3gwOLn5l9u7Seuj2V0YCA/edit?usp=sharing
Essentially I am using a Google script to "update" the spreadsheet titled "The copy to update". This copy will automatically copy all of the pages from "The copy to pull updates from", delete the current pages on "The copy to update", and then rename the copied sheets back to what they were before. This means that any changes I make to "The copy to pull updates from" will automatically be updated on the other spreadsheet without having to redistribute the spreadsheet.
When the button is pressed on the "Update page" sheet the pages are successfully transferred across. The changes are successfully "updated" on the to update spreadsheet. However, the page updated first in the code has errors. Any cell with a formula that acquires data from another sheets returns "#REF!" with the error "Unresolved sheet name 'Insert sheet name here'." This means that the formula/cell can not see the newly copied sheet from the script. I can remedy this situation by clicking on any cell afflicted by this and pressing enter - Essentially "refreshing" the cell. However I am doing this on a project with 100+ cells like this and I only want to have to press one button to be able to do this.
Should I try and solve this issue using script, or change my method of updating the Spreadsheet pages a different way? Thanks to all help in advance!
Here is the script that I am using:
<code>
function Update() {
//Sets the ID of the page to update from
var ID = "1R86_0SrAtyIJPAhMW0Ura-3gwOLn5l9u7Seuj2V0YCA"
//Pulls the correct sheet using the ID
var source = SpreadsheetApp.openById(ID);
//Identifies the active spreadsheet in use to update. (The spreadsheet you pressed "Update" on)
var destination = SpreadsheetApp.getActiveSpreadsheet();
//Sheet to pull and copy to the current spreadsheet
var sheet1 = source.getSheetByName("1");
//Copying the sheet accross
sheet1.copyTo(destination);
//Identifies the old copy of the sheet
var sheet1 = destination.getSheetByName('1');
//Deletes the old copy of the sheet
destination.deleteSheet(sheet1);
//Gets the new copy of the sheet
var sheet1 = destination.getSheetByName('Copy of 1');
//Renames the new copy of the sheet
sheet1.setName("1")
//Repeating the same code with page 2
//Sheet to pull and copy to the current spreadsheet
var sheet1 = source.getSheetByName("2");
//Copying the sheet accross
sheet1.copyTo(destination);
//Identifies the old copy of the sheet
var sheet1 = destination.getSheetByName('2');
//Deletes the old copy of the sheet
destination.deleteSheet(sheet1);
//Gets the new copy of the sheet
var sheet1 = destination.getSheetByName('Copy of 2');
//Renames the new copy of the sheet
sheet1.setName("2")
//Repeating the same code
}
</code>
This is related to Force formula refresh through Apps Script
I'm having the exact same problem. Most of my formulas are =ArrayFormula formulas in the first or second row. My solution iterates through all cells in the first two rows of a given Spreadsheet and reevalutes any formulas.
This could probably be cleaned up to only modify the cell if it is a formula (rather than resetting the text value for non-formulas). It could also be modified to run over an entire Spreadsheet by changing the assigned range.
You might run this function on the sheet after it has been copied to the new location, or you could call myFunction while the desired sheet is active.
function updateFormulasTwoRows(given_sheet) {
var last_col = given_sheet.getDataRange().getLastColumn();
var range = given_sheet.getRange(1,1,2,last_col);
// contains empty strings or cell formulas
var formulas = range.getFormulas();
// contains text, formula errors, formula output
var contents = range.getValues();
// formulas[0][0].length > 1 ==> is a formula
for (var r = 0; r < range.getHeight(); r++) {
for (var c = 0; c < range.getWidth(); c++) {
var cell = range.getCell(r+1,c+1);
// clear only the contents, not notes or comments or formatting.
cell.clearContent();
if (formulas[r][c].length > 1) {
// cell was a formula, so insert the formula back in place
cell.setValue(formulas[r][c]);
} else {
// cell was not a formula, so insert the text content back in place
cell.setValue(contents[r][c]);
}
}
}
}
// run this after selecting a sheet to reevalute formulas on
function myFunction() {
var curr_sheet = SpreadsheetApp.getActiveSheet();
updateFormulasTwoRows(curr_sheet);
}
Building on #bryan's answer above, I found a more efficient version of the code is the following, which performs the clearContent() and setValue() calls at the range level, rather than on each individual cell, thus saving a lot of calls to the back-end.
var updateFormulasAllRows = function updateFormulasAllRows(sheet) {
var range = sheet.getDataRange();
var formulas = range.getFormulas();
var contents = range.getValues();
var rangeWidth = range.getWidth(), rangeHeight = range.getHeight();
for (var r = 0; r < rangeHeight; r++) {
for (var c = 0; c < rangeWidth; c++) {
if (formulas[r][c].length > 1) {
contents[r][c] = formulas[r][c];
}
}
}
range.clearContent().setValues(contents);
};
This also changes the code to run over all the rows of the spreadsheet as suggested as an improvement.