I feel like today I'm not able to do anything with my script.
I'm trying to do a simple test, if the date in cell1 is equal to the date in cell2 then I will copy and paste the data into another file, in a specific sheet. If cell1 is different than cell2, I will copy the data in the same other file but in a different sheet.
I can't make it work. What am I doing wrong? thanks for the help,
j.
function copytransactionTodispatchN() {
var sss = SpreadsheetApp.getActiveSpreadsheet();; // sss = source spreadsheet
var ss = sss.getSheetByName("Nouveau Client - Transaction du jour"); // ss = source sheet
var ssn = sss.getSheetByName("Nouveau Client"); // ss = source sheet
//Get full range of data
var SRange = ss.getRange('A2:f2');
var SData = SRange.getValues();
var dateachat = ssn.getRange('c20').getValues;
var datelivraison = ssn.getRange('e31').getValues;
if(dateachat == datelivraison){
var tss = SpreadsheetApp.openById("idoftheotherfile"); // tss = target spreadsheet
var ts = tss.getSheetByName("sheet1"); // ts = target sheet
ts.getRange("b"+(getLastDataRow(ts)+1)+":i"+(getLastDataRow(ts)+1)).setValues(SData);
}
else {var tss = SpreadsheetApp.openById("idoftheotherfile"); // tss = target spreadsheet
var ts1 = tss.getSheetByName("Sheet2"); // ts = target sheet
//set the target range to the values of the source data
ts1.getRange("a"+(getLastDataRow(ts)+1)+":h"+(getLastDataRow(ts)+1)).setValues(SData);
}
}
function getLastDataRowN(sheet) {
var lastRow = sheet.getLastRow();
var range = sheet.getRange("b" + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
}
Ok, the mistakes were :
getlastrow function was not working for condition 2
the test of the date was not woking, I had to replace if(dateachat == datelivraison.) by if(dateachat.valueOf() == datelivraison.valueOf())
And now it is all good. Thank you all!!
var sss = SpreadsheetApp.getActiveSpreadsheet();; // sss = source spreadsheet
var ss = sss.getSheetByName("Nouveau Client - Transaction du jour"); // ss = source sheet
var ssn = sss.getSheetByName("Nouveau Client"); // ss = source sheet
//Get full range of data
var SRange = ss.getRange('A2:h2');
var SData = SRange.getValues();
var dateachat = ssn.getRange('c20').getValues;
var datelivraison = ssn.getRange('e31').getValues;
if(dateachat.valueOf() == datelivraison.valueOf()){
var tss = SpreadsheetApp.openById("IDtarget"); // tss = target spreadsheet
var ts = tss.getSheetByName("Sheet1"); // ts = target sheet
ts.getRange("b"+(getLastDataRowb(ts)+1)+":i"+(getLastDataRowb(ts)+1)).setValues(SData);
}
else {var tss = SpreadsheetApp.openById("IDtarget"); // tss = target spreadsheet
var ts = tss.getSheetByName("Sheet 2"); // ts = target sheet
//set the target range to the values of the source data
ts.getRange("a"+(getLastDataRowa(ts)+1)+":h"+(getLastDataRowa(ts)+1)).setValues(SData);
}
}
function getLastDataRowb(sheet) {
var lastRow = sheet.getLastRow();
var range = sheet.getRange("b" + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
}
function getLastDataRowa(sheet) {
var lastRow = sheet.getLastRow();
var range = sheet.getRange("a" + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
}```
var dateachat = ssn.getRange('c20').getValues;
Variable ssn isn't defined anywhere, which is likely causing dateachat to be undefined or null.
Related
I have the following code but I'm getting the following error after running it.
Is there any way to resolve this?
The data that I'm retrieving has about that amount of cell but it may be retrieving blank rows but all the rows has formulas.
N# of rows per file:
uno: 72850
dos: 72850
tres: 72850
cuatro: 72850
cinco: 72850
Total: 364250
function test() {
var dest = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('uno')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('dos')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id3')
var sh = ss.getSheetByName('tres')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id4')
var sh = ss.getSheetByName('cuatro')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id5')
var sh = ss.getSheetByName('cinco')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
};
The error message seems to be related to current Spreadsheets limits in Google Sheets as #Cooper mentioned, currently the limit is the following:
Up to 10 million cells or 18,278 columns (column ZZZ) for spreadsheets that are created in or converted to Google Sheets.
Creating a new sheet could do the work as the limits apply per sheets.
Try to replace getLastRow() by getLastDataRow() after adding
Object.prototype.getLastDataRow = function(col){
var lastRow = this.getLastDataRow();
if (col == null){col='A'}
var range = this.getRange(col + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
so ...
function test() {
var dest = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('uno')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('dos')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id3')
var sh = ss.getSheetByName('tres')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id4')
var sh = ss.getSheetByName('cuatro')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id5')
var sh = ss.getSheetByName('cinco')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
};
Object.prototype.getLastDataRow = function(col){
var lastRow = this.getLastDataRow();
if (col == null){col='A'}
var range = this.getRange(col + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
I tried to fill it with my sheet data, but that
var dataRange = sheet.getDataRange();
get an error. What should I do?
function RefreshImports() {
var lock = LockService.getScriptLock();
if (!lock.tryLock(5000)) return; // Wait up to 5s for previous refresh to end.
var id = "1r7TgCWXfjmcjufT0yz9qcFDlr482SdHDHlZYblXyAt0"; // [YOUR SPREADSHEET ID]
var ss = SpreadsheetApp.openById(id);
var sheet = ss.getSheetByName("crypto"); // sheet name
var dataRange = sheet.getDataRange();
var formulas = dataRange.getFormulas();
var content = "";
var now = new Date();
var time = now.getTime();
var re = /.*[^a-z0-9]import(?:xml|data|feed|html|range)\(.*/gi;
var re2 = /((\?|&)(update=[0-9]*))/gi;
var re3 = /(",)/gi;
for (var row=0; row<formulas.length; row++) {
for (var col=0; col<formulas[0].length; col++) {
content = formulas[row][col];
if (content != "") {
var match = content.search(re);
if (match !== -1 ) {
// import function is used in this cell
var updatedContent = content.toString().replace(re2,"$2update=" + time);
if (updatedContent == content) {
// No querystring exists yet in url
updatedContent = content.toString().replace(re3,"?update=" + time + "$1");
}
// Update url in formula with querystring param
sheet.getRange(row+1, col+1).setFormula(updatedContent);
}
}
}
}
// Done refresh; release the lock.
lock.releaseLock();
// Show last updated time on sheet somewhere
sheet.getRange(7,2).setValue("Rates were last updated at " + now.toLocaleTimeString())
}
I set up the trigger.
Not sure what you trying to do and you explanation leaves a lot to be desired.
So this is a simple way to put your formulas into a sheet named "Destination".
function RefreshImports() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("crypto");
const rg = sh.getDataRange();
const formulas = rg.getFormulas();
sh.getRange(1,1,formulas.length,formulas[0].length).setFormulas(formulas);
}
I want to create a backup sheet of a google sheet file and its name should be "previous month name + year". Backup file is created, renaming the file is not happening.
function Copy() {
var sss =
SpreadsheetApp.openById('1IK6YsZS3_NNAlFA41d8uKriPzfZs_2Ukyh94eeBFj40');
var ss = sss.getSheetByName('CURRENT');
var range = ss.getRange('A1:H45');
var data = range.getValues();
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var yourNewSheet = activeSpreadsheet.getSheetByName("prevMonth");
if (yourNewSheet != null)
{
activeSpreadsheet.deleteSheet(yourNewSheet);
}
yourNewSheet = activeSpreadsheet.insertSheet();
yourNewSheet.setName("prevMonth");
yourNewSheet.getRange(yourNewSheet.getLastRow()+1,1,45,8).setValues(data);
}
By using the code below:
function Copy() {
var sss = SpreadsheetApp.openById('1IK6YsZS3_NNAlFA41d8uKriPzfZs_2Ukyh94eeBFj40');
var ss = sss.getSheetByName('CURRENT');
var range = ss.getRange('A1:H45');
var data = range.getValues();
var d = new Date();
d.setMonth(d.getMonth() - 1);
var prevMonth = Utilities.formatDate(d, 'GMT+1', 'MMMM yyyy');
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var yourNewSheet = activeSpreadsheet.getSheetByName(prevMonth);
if (yourNewSheet != null) {
activeSpreadsheet.deleteSheet(yourNewSheet);
}
yourNewSheet = activeSpreadsheet.insertSheet();
yourNewSheet.setName(prevMonth);
yourNewSheet.getRange(yourNewSheet.getLastRow()+1,1,45,8).setValues(data);
}
It will create a Sheet named "October 2019".
I have a google sheet with 2 tabs, BOMSheet & POProcess. I want to copy the PO Number from PO Process sheet by looking up the column B values of POProcess sheet (one by one continuously) in column D values of BOM Sheet and if matches then pasting the PO number in column M of the BOM sheet (for all values that are matched). here is the link of the sheet.
https://docs.google.com/spreadsheets/d/1MqYn2AjPncx-RvvyTS8Nj0ujpm9Lcd_ORJF9Wqbw6y0/edit?usp=sharing
I tried the code below but it's not working as its checking up the only one value from the PO Process sheet. Please help how to iterate the values from PO process sheet
function UpdateStatus() {
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ss = sss.getSheetByName('POProcess'); //replace with source Sheet tab name
var range = ss.getRange('C4'); //assign the range you want to copy
var data = range.getValues();
var range2 = ss.getRange('B8'); //value to look for to be replaced
var data2 = range2.getValues();
var sheet = sss.getSheetByName('BOMSheet');
var range3 = sheet.getRange('A:L');
var values = range3.getValues();
for (var i = 0; i < values.length; i++) {
if (values[i][3] == data2) {
values[i][12] = data;
}
}
range3.setValues(values);
}
You can use this function for only PO number.
function UpdateStatus() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pSheet = ss.getSheetByName('POProcess'); //replace with source Sheet tab name
var pNumber = pSheet.getRange('C4').getValue(); //assign the range you want to copy
var pRange = pSheet.getRange(8, 2, pSheet.getLastRow()-7, 1); //value to look for to be replaced
var pData = pRange.getValues().map(function(el) {
return el[0];
});
var sheet = ss.getSheetByName('BOMSheet');
var range3 = sheet.getDataRange();
var values = range3.getValues();
values.forEach(function(row) {
if (row[3] == '') return;
if (pData.indexOf(row[3]) > -1) {
row[12] = pNumber;
}
});
range3.setValues(values);
}
For mapping 'Qty Received' column also, try this.
function UpdateStatus() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pSheet = ss.getSheetByName('POProcess'); //replace with source Sheet tab name
var pNumber = pSheet.getRange('C4').getValue(); //assign the range you want to copy
var pRange = pSheet.getRange(8, 2, pSheet.getLastRow() - 7, pSheet.getLastColumn()-1); //values to map
var pData = {};
pRange.getValues().forEach(function(row) {
pData[row[0]] = row[6];
});
var sheet = ss.getSheetByName('BOMSheet');
var range3 = sheet.getDataRange();
var values = range3.getValues();
values.forEach(function(row) {
if (row[3] == '') return;
if (typeof pData[row[3]] != 'undefined') {
row[12] = pNumber;
row[13] = pData[row[3]];
}
});
range3.setValues(values);
}
I have written a script to clean up and move data between 3 different sheets. A user first paste a data extract on to the "Extract" page, and then runs the script.
When a user paste the data into cell A1 on the "Extract" page, I would like a prompt box to ask the user if they would like to run the script; if Yes, run script, if No, don't run script and show message. How would I go about doing this?
This is what I have so far...everything after the first function works.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Extract");
if(ss.s().getRange("A1").getValue() != ""){
var ui = SpreadsheetApp.getUi()
var response = ui.alert('Would you jbot to find new claims for you?', ui.ButtonSet.OK_CANCEL);
if (response == ui.Button.OK) {
jbot();
} else {
Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.');
}
}
}
function jbot(){
movetobot();
deleteRows();
removeDupesInOtherSheets();
deleteCol();
cleanup();
movetoqueue();
message();
};
function movetobot() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Extract");
var ts = ss.getSheetByName("bot");
s.getRange("A1:BW").moveTo(ts.getRange("A1"));
}
function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("bot");
var r = s.getRange("BO:BO");
var v = r.getValues();
for(var i=v.length-1;i>=0;i--)
if(v[0,i]=='#POSTLESSEE' || v[0,i]=='#TDI CRC_OANKURA' || v[0,i]=='#Partpaymentoffer' || v[0,i]=='#TDI_CRC_DVANKURA' || v[0,i]=='#partpaymentdupe' )
s.deleteRow(i+1);
}
function removeDupesInOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("bot").getDataRange().getValues();
var s2 = ss.getSheetByName("Queue").getDataRange().getValues();
// iterate 'Queue' and check in 'bot' if duplicate values exist
var nS1 = [];
var s2Col1 = [];// data in column1 of Queue
for(var n in s2){
s2Col1.push(s2[n][0]);
}
for(var n in s1){ // iterate '180418970' and test col 1 vs col 1 in 'Queue'
var noDup1 = checkForDup(s1[n],s2Col1)
if(noDup1){nS1.push(noDup1)};// if not present in 'Queue' then keep
}
Logger.log(nS1);// view result
ss.getSheetByName("bot").getDataRange().clear();// clear and update sheets
ss.getSheetByName("bot").getRange(1,1,nS1.length,nS1[0].length).setValues(nS1);
}
function checkForDup(item,s){
Logger.log(s+' = '+item[0]+' ?')
if(s.indexOf(item[0])>-1){
return null;
}
return item;
}
function deleteCol() {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
var lastCol = sheet.getLastColumn();
var keep = [1,2,3,4,45,53,74, 75]; // array of column numbers to keep
for (var col=lastCol; col > 0; col--) {
if (keep.indexOf(col) == -1) {
// This isn't a keeper, delete it
sheet.deleteColumn(col);
}
}
}
function cleanup() {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
sheet.getRange("B1:B").moveTo(sheet.getRange("H1"));
sheet.deleteColumn(2)
sheet.deleteRow(1)
var cell = sheet.getRange("D:D");
cell.setNumberFormat("m/d/yy");
var cell = sheet.getRange("F:F");
cell.setNumberFormat("m/d/yy");
var cell = sheet.getRange("A:A");
cell.setHorizontalAlignment("center");
var cell = sheet.getRange("C:D");
cell.setHorizontalAlignment("center");
var cell = sheet.getRange("F:G");
cell.setHorizontalAlignment("center");
sheet.autoResizeColumn(2)
sheet.autoResizeColumn(5)
sheet.autoResizeColumn(7)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
var cell = sheet.getRange("BW1");
cell.setFormula("=COUNT(A:A)");
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
var range = sheet.getRange("A1:G");
range.sort({column: 4, ascending: true})
}
function movetoqueue() {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
// skip row 1 (header) and beyond column G
var range = sheet.getRange(1,1,sheet.getLastRow()-1,7);
sheet = spread.getSheetByName("Queue");
var rows = sheet.getRange(1,1,sheet.getLastRow(),1).getValues();
// search for first blank row column 1
for( var i=0; i<rows.length; i++ ) {
if( rows[i][0] === "" ) {
var offset = sheet.getRange(1,1).offset(i,0);
range.copyTo(offset);
break;
}
}
}
function message() {
SpreadsheetApp.getActive().getSheetByName("Queue").activate();
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("bot");
var range = sheet.getRange("bot!BW1:BW1");
var data = range.getValue();
Browser.msgBox(data + " new claims have been added to the queue. Thank you for using jBot!");
}
Your onEdit(e) will trigger the alert regardless of which cell is edited. You need to check the event range to limit it to cell A1 of Extract like this.
function onEdit(e) {
if( e.range.getSheet().getName() === "Extract" ) {
if( e.range.getA1Notation() === "A1" ) {
if( e.value !== "" ) {
var ui = SpreadsheetApp.getUi();
// Do the rest of your stuff here
}
}
}
}