Issue with Google Apps Script code copy-paste from Gsheet file - database

I've created a code to copy-paste the values from the most recent uploaded Gsheet file to another. The code is supposed to copy-paste only values that fulfil a cell condition.
The problem is, this code takes to long to run and doesn't finish to copy all values before having a run-time error.
The file has about 300.000 cells and the final amounts to be pasted on the 2nd file have about 90.000 cells.
Does anyone have a recommendation on how to proceed?
Thank you so much for your kind support.
function Master_Run_DB() {
Copy_EUDB();
}
function Copy_EUDB() {
var myLink = extractLink_EUDB();
sendData_EUDB(myLink);
}
function extractLink_EUDB() {
var newData = new Date().toLocaleString();
var drive = DriveApp.getFolderById("link to file");
var file = drive.getFilesByType(MimeType.GOOGLE_SHEETS);
var link = file.next().getUrl();
var ss = SpreadsheetApp.getActiveSpreadsheet();
return link
}
function sendData_EUDB(link) {
var mainSS = SpreadsheetApp.openById("link to file");
var lastRow = mainSS.getSheetByName("Database").getLastRow();
mainSS.getSheetByName("Database").getRange("A2" + ":R" + lastRow).clearContent();
var baseSS = SpreadsheetApp.openByUrl(link);
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getUrl());
var lRow = baseSS.getSheetByName("Sheet1").getLastRow();
for (var i = 2; i <= lRow; i++) {
var cell = baseSS.getRange("G" + i);
var val = cell.getValue();
if (val == "EU") {
var sourceData = baseSS.getSheetByName("Sheet1").getRange("A" + i + ":R" + i).getValues();
var to = mainSS.getSheetByName("Database").getRange("A" + i + ":R" + i).setValues(sourceData);
}
}
};
Hello again,
Now that the new code is working, I was trying to add an IF AND clause to the copy-paste. The objective would be to copy only values that fulfil the following condition:
Column 7 = "EU"
Column 11 <> "CCCC"
But I'm getting a Syntax Error. Any advice?
Thank you in advance for your kind support
function Master_Run_EU() {
var drive = DriveApp.getFolderById("link to file");
var file = drive.getFilesByType(MimeType.GOOGLE_SHEETS);
var link = file.next().getUrl();
var ss = SpreadsheetApp.openById("link to file");
const sh=ss.getSheetByName('Database');
sh.getRange(2,1,sh.getLastRow(),22).clearContent();
var dbss = SpreadsheetApp.openByUrl(link);
const dbsh=dbss.getSheetByName('Sheet1');
var oA=[];
var vs=dbsh.getRange(2,1,dbsh.getLastRow()-1,22).getValues();
vs.forEach(function(r,i){
if(r[7]=="EU" && r[11]<>"CCCC") {
oA.push(r);
}
});
sh.getRange(2,1,oA.length,oA[0].length).setValues(oA);
}

Try this:
function Master_Run_DB() {
var drive = DriveApp.getFolderById("1ViOyzIkGOI6G6SMrtmPv4vjL-2-2duHC");
var file = drive.getFilesByType(MimeType.GOOGLE_SHEETS);
var link = file.next().getUrl();
var ss = SpreadsheetApp.openById("13CchyoqiWyvGTnYuG5TWA4cggBS-FsoDkW8XGesDkiY");
const sh=ss.getSheetByName('Database');
sh.getRange(2,1,sh.getLastRow(),18).clearContent();
var dbss = SpreadsheetApp.openByUrl(link);
const dbsh=dbss.getSheetByName('Sheet1');
var vs=dbsh.getRange(2,1,dbsh.getLastRow()-1,18).getValues();
vs.forEach(function(r,i){
if(r[6]=="EU") {
sh.getRange(i+2,1,1,18).setValues([r]);
}
});
}
It would be faster to do it this way:
function Master_Run_DB() {
var drive = DriveApp.getFolderById("1ViOyzIkGOI6G6SMrtmPv4vjL-2-2duHC");
var file = drive.getFilesByType(MimeType.GOOGLE_SHEETS);
var link = file.next().getUrl();
var ss = SpreadsheetApp.openById("13CchyoqiWyvGTnYuG5TWA4cggBS-FsoDkW8XGesDkiY");
const sh=ss.getSheetByName('Database');
sh.getRange(2,1,sh.getLastRow(),18).clearContent();
var dbss = SpreadsheetApp.openByUrl(link);
const dbsh=dbss.getSheetByName('Sheet1');
var oA=[];
var vs=dbsh.getRange(2,1,dbsh.getLastRow()-1,18).getValues();
vs.forEach(function(r,i){
if(r[6]=="EU") {
oA.push(r);
}
});
sh.getRange(2,1,oA.length,oA[0].length).setValues(oA);
}

Related

I am getting an error running this code loading data from excel sheet into google firebase using google cloud service account both of same mail

function getFirestore() {
var email="busdata-account#export-815a4.iam.gserviceaccount.com";
var key = "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDIBWAXxbEGco/p\nr6sEuBWiHJTayXcxD5bQjJNeMCHFnf3DlYxS9HPw3TG/vJBqwIMWKgd9waO9l4M1\nz3+P7obaOJ9OuwV30ZXrzE2F3+i8rnF009HxFGYIpHl3m6QffJ2nLnadfSik7HSv\nhDXHhQodgLkGidLXjLhPwb1hI7Cq96HFPPYqTu1oZqDgl9KMwyI+YgYIhoM+j4uo\n8nYkPErA2Q4hc3cI78Yl1mxAB5Ew1hnN8sPv3jtiXSm1irWI1MkITYcBteT9jRVm\ngR2Bvwu59XCqiX7jJ3fB41DpJcZq9oX6GpWfh846gKio0jaTGcjQx3SmlVUCEkgn\nIv/RKvy7AgMBAAECggEAYaqiQ6lPqa7Jz5jrwDektI4RLw2dihKrGPTzRlPpQaXG\nEbjVlnHvpOZU0uMff0i2GV33X3PsYHh5ZI6Hon+dUDPCsf5HCr0HX+ZX2i09Ztgk\n5R9CzV0g/0pUV09joc5G6qmY2IiTDoi45zT+z4gKKuK+Nc+ZaI+5NZSLxMFZE/lN\n2c6ANbDXA5aezAzit38LcAZ0QLp4VsOY4Hlh+JYjVrZNAbtllRYkuK7fq7bus+fs\nBbqWvKgqjChLwVu8efuUiOvN/kjV84TqRov9Kg7b+7ozl3HSYOfw6NTE9M/aZ54l\naav+YAnoBSWmgG2NuhOmzs2yFf1VctEmRxquKMZ1hQKBgQD0gCETgMxocFGedk1P\nxzL+0plIfnav9GK2n96Pi0rpnyId7xpDdVlfBnkwplgPHDELRIpdbPyjz4408Jn4\ndoV2jSsUKR4ziDxRos2MKFDqA7MJwLYgNa/1hqfpF8bbYR5g7vpPZyT3/gWC/Dni\nndUMIMq+ikxgv70RGXVSh21cLQKBgQDRbbKODtT03CIofDioLyCWsM/aoyvwvEQP\nBaCx7CkkpVXeuosHua9jUekfsSsbWwTGnuP9o5DDgpwkQVzNXIxZXm6rCVZP9PqC\n/mgKDRNAtfU5g07WWpoAbFiJ7b1nJQ40H+sifx5kucDz8DewMAZJdiwKHZ4a9RG9\nRfdEAYyFhwKBgDXGUIAHne2CdpERl+Np/m9JRmlJVzVP3L4p3ztW+Xz0gP5wbihU\nEPnzZIgsrcV0/9xokL4HkHE+3xoJqPOarbPrTIQ8nuXamtmhou+mllVBxwjoF6Zx\n6Gdp3FhJ4HSzoYjURr1cBCd++Rtb+ic7a3YlaDIMHMxSn3S5UrJ6ErVpAoGBALiN\n2/C1/1J8fxmARolnZmeFDtZUPs+ZiLxYMEE6pRMnJCHv777yEi4LK84C2cL0laPW\nt4F314WDHsgQVZCfgwrqXcoZo1c4wl4XhWV+Gj5HWv/AyAO7ucSvteJugB9aF0c1\n7Ybr0MPgsboGMXzxBUlNewe7C+4jkuthscHbP5MXAoGAUL6S6UEXvl1I1a3xbvHo\nv5wv+5fiP4ODq2XgNs6z4gFfTUrjb3CryHqg4I1OYPmq2d8UZ5mDkXPoSXmuz6Wr\nm57NW3d5Oio17vk2IXrzIFZ1MKEm8qu2aLzBCOEeY/fge6Lkyx0Z6ALpwbjPakZD\nrZLeFrU13pKQHKmLQknnoqk=\n-----END PRIVATE KEY-----\n";
var projectId = "export-815a4";;
return FirestoreApp.getFirestore(email,key,projectId);
}
function myFunction() {
const firestore=getFirestore();
var ss = SpreadSheetApp.getActiveSpreadsheet();
var sheetname = "busData";
var sheet = ss.getSheetByName(sheetname);
var lastRow = sheet.getLastRow();
var lastCol = sheet.getLastColumn();
var dataSR = 5;
var sheetRange = sheet.getRange(2,1,lastRow-dataSR+1,lastCol);
var sheetData = sheetRange.getValues();
var sourceLen = sheetData.length;
for(var i=0;i<sourceLen;i++){
if(sheetData[i][1] != ' '){
var data = {};
var sheetData = sheetRange[i][0];
data.place =sheetData[i][1];
data.busId= sheetData[i][2];
data.timeValue = sheetData[i][3];
data.hour = sheetData[i][4];
data.minute = sheetData[i][5];
data.slot = sheetData[i][6];
Logger.log(data);
firestore.createDocument("busData",data);
}
}
}
//enter image description here
//here is my excel sheet link: i am trying to load into firebase its giving an error //of semi-colomn but every semi-column is in its place . ac any one help.
//here is my excel sheet link:[text]//(https://docs.google.com/spreadsheets/d/1zVaLfQ1YC-aKJwzDAHmIBlzc2YfU4GpDnubBnNhOcRc/edit?usp=sharing)
Can anyone help me? I am doing this watching a Youtube video and I am a beginner.

getRange of null error on code that previously worked

I searched the other instances of people reporting a "getRange" of null error, but none of them solved my issue. I had a form built in Google Sheets, and the code worked perfectly this morning until I made an inadvertent edit, tried to undo the change, but nonetheless I must have done something because the script stopped working.
The script is meant to:
Allow a user to create a record by inputing information using code 1000 on the Form sheet, and to save such information to the Data sheet
Display a record's information from the Data sheet on the Form sheet when the user enters a code greater than 1000 (a record locator ID)
Permit the user to update and save information for the record displayed
Reset the form to blank when either the new record is created, or the existing record has been updated.
It worked perfectly this morning, then kaput. I've been trying to debug unsuccessfully for hours, and here are error messages by function:
submitData
TypeError: Cannot read property 'getRange' of null (line 98, file
"Code")
getMember
Exception: Range not found (line 249, file "Code")
onEdit
TypeError: Cannot read property 'getActiveCell' of null (line 378,
file "Code")
I've been through every line of code, and I am stuck. I humbly request another pair of eyes to help me fix it. Below is the code, and here is a link to a copy of the form and data table I created with a sample row of data. Please ignore the data validation errors since I deleted proprietary information to create this copy, and please let me know if you have any questions. Thank you.
'''
//array for setValue on Data Centre and getValue by column from database
var cellId = ["J3","A"];
var cellPhoto = ["J32","B"];
//Location
var cellStreetAddress = ["D7","C"];
var cellCity = ["D8","D"];
var cellState = ["D9","E"];
var cellZip = ["D10","F"];
var cellCounty = ["D11","G"];
//Program data
var cellfiftyfive = ["H7","H"];
var cellIL = ["H8","I"];
var cellAL = ["H9","J"];
var cellMC = ["H10","K"];
var cellBeds = ["H12","L"];
//Property Summary
var cellPhase = ["K7","M"];
var cellLand = ["K8","N"];
var cellBuildable = ["K9","O"];
var cellPrice = ["K10","P"];
var cellGrade = ["K11","Q"];
var cellSource = ["K12","R"];
var cellClassification = ["K13","S"];
//Description
var cellSiteDescription = ["D16","T"];
var cellProjDescription = ["D21","U"];
//Site Information
var cellSiteVisit = ["L16","V"];
var cellTeamsLink = ["K17","W"];
var cellGoogleMap = ["K18","X"];
//Zoning
var cellZoningClass = ["K21","Y"];
var cellZoningDesc = ["K22","Z"];
var cellEntitlements = ["K23","AA"];
//Construction
var cellParkingType = ["K26","AB"];
var cellConstType = ["K27","AC"];
var cellUnitSizeRange = ["K28","AD"];
var cellTotalSqFt = ["K29","AE"];
//Financial
var cellTotalCost = ["D32","AF"];
var cellTotalCostDoor = ["D33","AG"];
var cellTotalCostSF = ["D34","AH"];
var cellTotalDebt = ["D35","AI"];
var cellTotalEquity = ["D36","AJ"];
var cellFDGEquity = ["D37","AK"];
var cellCapitalStructure = ["D38","AL"];
//EARLY
var cellDateProcessed = ["H42","AM"];
var cellMarketStudyLink = ["H43","AN"];
//Site Photo
var cellPhotoLibrary = ["K43","AO"];
//Stakeholders
var cellCoDev = ["D46","AP"];
var cellFeeDev = ["D47","AQ"];
var cellOperator = ["D48","AR"];
var cellPartner = ["D49","AS"];
var cellArchitect = ["D50","AT"];
var cellEngineer = ["D51","AU"];
var cellContractor = ["D52","AV"];
var cellBroker = ["D53","AW"];
//Schedule
var cellRezoning = ["H46","AX"];
var cellSitePlan = ["H48","AY"];
var cellPlanPermit = ["H49","AZ"];
var cellClosing = ["H50","BA"];
var cellExtensions = ["H51","BB"];
var cellTimeToClose = ["H52","BC"];
var cellEstConstStart = ["H54","BD"];
var cellEstConstLength = ["H55","BE"];
var cellEstConstFinish = ["H56","BF"];
//Additional Notes
var cellNotes = ["J46","BG"];
var cellDropped = ["J55","BH"];
//Date Stamps
var cellInputDate = ["L3","BI"];//var cellInputDate = ["D23","K"];
var cellUpdateDate = ["L4","BJ"];//var cellUpdateDate = ["H23","L"];
function submitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form");
var datasheet = ss.getSheetByName("Data");
var lastrow = datasheet.getLastRow();
var myId = formSS.getRange(cellId[0]).getValue();
var maxId = findMaxId(datasheet.getRange("A2:" + "A" + lastrow).getValues());
var nextId = increaseId(maxId);
var lastRowId = datasheet.getRange(lastrow,1,1,1).getValue();
var nextId = increaseId(lastRowId);
var myId = formSS.getRange(cellId[0]).getValue();
var now = new Date();
//Location
var my_StreetAddress = formSS.getRange(cellStreetAddress[0]).getValue();
var my_City = formSS.getRange(cellCity[0]).getValue();
var my_State = formSS.getRange(cellState[0]).getValue();
var my_Zip = formSS.getRange(cellZip[0]).getValue();
var my_County = formSS.getRange(cellCounty[0]).getValue();
//Program
var my_fiftyfive = formSS.getRange(cellfiftyfive[0]).getValue();
var my_IL = formSS.getRange(cellIL[0]).getValue();
var my_AL = formSS.getRange(cellAL[0]).getValue();
var my_MC = formSS.getRange(cellMC[0]).getValue();
var my_Beds = formSS.getRange(cellBeds[0]).getValue();
//Property Summary
var my_Phase = formSS.getRange(cellPhase[0]).getValue();
var my_Land = formSS.getRange(cellLand[0]).getValue();
var my_Buildable = formSS.getRange(cellBuildable[0]).getValue();
var my_Price = formSS.getRange(cellPrice[0]).getValue();
var my_Grade = formSS.getRange(cellGrade[0]).getValue();
var my_Source = formSS.getRange(cellSource[0]).getValue();
var my_Classification = formSS.getRange(cellClassification[0]).getValue();
//Description
var my_SiteDescription = formSS.getRange(cellSiteDescription[0]).getValue();
var my_ProjDescription = formSS.getRange(cellProjDescription[0]).getValue();
//Site Information
var my_SiteVisit = formSS.getRange(cellSiteVisit[0]).getValue();
var my_TeamsLink = formSS.getRange(cellTeamsLink[0]).getValue();
var my_GoogleMap = formSS.getRange(cellGoogleMap[0]).getValue();
//Zoning
var my_ZoningClass = formSS.getRange(cellZoningClass[0]).getValue();
var my_ZoningDesc = formSS.getRange(cellZoningDesc[0]).getValue();
var my_Entitlements = formSS.getRange(cellEntitlements[0]).getValue();
//Construction
var my_ParkingType = formSS.getRange(cellParkingType[0]).getValue();
var my_ConstType = formSS.getRange(cellConstType[0]).getValue();
var my_UnitSizeRange = formSS.getRange(cellUnitSizeRange[0]).getValue();
var my_TotalSqFt = formSS.getRange(cellTotalSqFt[0]).getValue();
//Financial
var my_TotalCost = formSS.getRange(cellTotalCost[0]).getValue();
var my_TotalCostDoor = formSS.getRange(cellTotalCostDoor[0]).getValue();
var my_TotalCostSF = formSS.getRange(cellTotalCostSF[0]).getValue();
var my_TotalDebt = formSS.getRange(cellTotalDebt[0]).getValue();
var my_TotalEquity = formSS.getRange(cellTotalEquity[0]).getValue();
var my_FDGEquity = formSS.getRange(cellTotalEquity[0]).getValue();
var my_CapitalStructure = formSS.getRange(cellCapitalStructure[0]).getValue();
//EARLY
var my_DateProcessed = formSS.getRange(cellDateProcessed[0]).getValue()
var my_MarketStudyLink = formSS.getRange(cellMarketStudyLink[0]).getValue()
//Site Photo
var my_Photo = formSS.getRange(cellPhoto[0]).getValue();
var my_PhotoLibrary = formSS.getRange(cellPhotoLibrary[0]).getValue();
//Stakeholders
var my_CoDev = formSS.getRange(cellCoDev[0]).getValue();
var my_FeeDev = formSS.getRange(cellFeeDev[0]).getValue();
var my_Operator = formSS.getRange(cellOperator[0]).getValue();
var my_Partner = formSS.getRange(cellPartner[0]).getValue();
var my_Architect = formSS.getRange(cellArchitect[0]).getValue();
var my_Engineer = formSS.getRange(cellEngineer[0]).getValue();
var my_Contractor = formSS.getRange(cellContractor[0]).getValue();
var my_Broker = formSS.getRange(cellBroker[0]).getValue();
//Schedule
var my_Rezoning = formSS.getRange(cellRezoning[0]).getValue();
var my_SitePlan = formSS.getRange(cellSitePlan[0]).getValue();
var my_PlanPermit = formSS.getRange(cellPlanPermit[0]).getValue();
var my_Closing = formSS.getRange(cellClosing[0]).getValue();
var my_Extensions = formSS.getRange(cellExtensions[0]).getValue();
var my_TimeToClose = formSS.getRange(cellTimeToClose[0]).getValue();
var my_EstConstStart = formSS.getRange(cellEstConstStart[0]).getValue();
var my_EstConstLength = formSS.getRange(cellEstConstLength[0]).getValue();
var my_EstConstFinish = formSS.getRange(cellEstConstFinish[0]).getValue();
//Additional Notes
var my_Notes = formSS.getRange(cellNotes[0]).getValue();
var my_Dropped = formSS.getRange(cellDropped[0]).getValue();
if (myId == 1000) { //input mode
var values = [
nextId,'',my_StreetAddress,my_City,my_State,my_Zip,my_County,my_fiftyfive,my_IL,my_AL,my_MC,my_Beds,my_Phase,my_Land,my_Buildable,my_Price,my_Grade,
my_Source,my_Classification,my_SiteDescription,my_ProjDescription,my_SiteVisit,my_TeamsLink,my_GoogleMap,my_ZoningClass,my_ZoningDesc,my_Entitlements,
my_ParkingType,my_ConstType,my_UnitSizeRange,my_TotalSqFt,my_TotalCost,my_TotalCostDoor,my_TotalCostSF,my_TotalDebt,my_TotalEquity,my_FDGEquity,my_CapitalStructure,
my_DateProcessed,my_MarketStudyLink,my_PhotoLibrary,my_CoDev,my_FeeDev,my_Operator,my_Partner,my_Architect,my_Engineer,
my_Contractor,my_Broker,my_Rezoning,my_SitePlan,my_PlanPermit,my_Closing,my_Extensions,my_TimeToClose,
my_EstConstStart,my_EstConstLength,my_EstConstFinish,my_Notes,my_Dropped,now
];
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 61).setValues(values);
clear_screen();
} else { //update mode
var rowIndex = findInRow(myId);
var rangesToSetValues = [cellStreetAddress[1], cellCity[1], cellState[1], cellZip[1], cellCounty[1], cellfiftyfive[1], cellIL[1],
cellAL[1], cellMC[1], cellBeds[1], cellPhase[1], cellLand[1], cellBuildable[1],
cellPrice[1], cellGrade[1], cellSource[1], cellClassification[1], cellSiteDescription[1], cellProjDescription[1],
cellSiteVisit[1], cellTeamsLink[1], cellGoogleMap[1], cellZoningClass[1], cellZoningDesc[1], cellEntitlements[1],
cellParkingType[1], cellConstType[1], cellUnitSizeRange[1], cellTotalSqFt[1],cellTotalCost[1], cellTotalCostDoor[1], cellTotalCostSF[1],
cellTotalDebt[1], cellTotalEquity[1], cellFDGEquity[1], cellCapitalStructure[1], cellDateProcessed[1], cellMarketStudyLink[1], cellPhotoLibrary[1],
cellCoDev[1], cellFeeDev[1], cellOperator[1], cellPartner[1], cellArchitect[1], cellEngineer[1],
cellContractor[1], cellBroker[1], cellRezoning[1], cellSitePlan[1], cellPlanPermit[1], cellClosing[1],
cellExtensions[1], cellTimeToClose[1], cellEstConstStart[1], cellEstConstLength[1], cellEstConstFinish[1], cellNotes[1],cellDropped[1]];
var values = [
my_StreetAddress,my_City,my_State,my_Zip,my_County,my_fiftyfive,my_IL,my_AL,my_MC,my_Beds,my_Phase,my_Land,my_Buildable,my_Price,my_Grade,
my_Source,my_Classification,my_SiteDescription,my_ProjDescription,my_SiteVisit,my_TeamsLink,my_GoogleMap,my_ZoningClass,my_ZoningDesc,my_Entitlements,
my_ParkingType,my_ConstType,my_UnitSizeRange,my_TotalSqFt,my_TotalCost,my_TotalCostDoor,my_TotalCostSF,my_TotalDebt,my_TotalEquity,my_FDGEquity,my_CapitalStructure,
my_DateProcessed,my_MarketStudyLink,my_PhotoLibrary,my_CoDev,my_FeeDev,my_Operator,my_Partner,my_Architect,my_Engineer,
my_Contractor,my_Broker,my_Rezoning,my_SitePlan,my_PlanPermit,my_Closing,my_Extensions,my_TimeToClose,my_EstConstStart,my_EstConstLength,
my_EstConstFinish,my_Notes,my_Dropped
];
for (var i=0; i<rangesToSetValues.length; i++) {
datasheet.getRange(rangesToSetValues[i]+rowIndex).setValue(values[i]);
}
//set lastupdateDate value
datasheet.getRange(cellUpdateDate[1]+rowIndex).setValue(now);
getMember(myId);
}
}
function getMember(id) {
var sheetData = SpreadsheetApp.getActive().getSheetByName('Form');
var rangesToSetValues = [cellStreetAddress[0], cellCity[0], cellState[0], cellZip[0], cellCounty[0], cellfiftyfive[0], cellIL[0],
cellAL[0], cellMC[0], cellBeds[0], cellPhase[0], cellLand[0], cellBuildable[0],
cellPrice[0], cellGrade[0], cellSource[0], cellClassification[0], cellSiteDescription[0], cellProjDescription[0],
cellSiteVisit[0], cellTeamsLink[0], cellGoogleMap[0], cellZoningClass[0], cellZoningDesc[0], cellEntitlements[0],
cellParkingType[0], cellConstType[0], cellUnitSizeRange[0], cellTotalSqFt[0], cellTotalCost[0], cellTotalCostDoor[0], cellTotalCostSF[0],
cellTotalDebt[0], cellTotalEquity[0], cellFDGEquity[0], cellCapitalStructure[0], cellDateProcessed[0], cellMarketStudyLink[0], cellPhotoLibrary[0],
cellCoDev[0], cellFeeDev[0], cellOperator[0], cellPartner[0], cellArchitect[0], cellEngineer[0],
cellContractor[0], cellBroker[0], cellRezoning[0], cellSitePlan[0], cellPlanPermit[0], cellClosing[0],
cellExtensions[0], cellTimeToClose[0], cellEstConstStart[0], cellEstConstLength[0], cellEstConstFinish[0], cellNotes[0], cellDropped[0],
cellInputDate[0], cellUpdateDate[0]];
var rowIndex = findInRow(id);
var sheetDatabase = SpreadsheetApp.getActive().getSheetByName('Data');
var values = [
//Location
sheetDatabase.getRange(cellStreetAddress[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellCity[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellState[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellZip[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellCounty[1]+rowIndex).getValue(),
//Program data
sheetDatabase.getRange(cellfiftyfive[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellIL[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellAL[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellMC[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellBeds[1]+rowIndex).getValue(),
//Property Summary
sheetDatabase.getRange(cellPhase[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellLand[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellBuildable[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellPrice[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellGrade[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellSource[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellClassification[1]+rowIndex).getValue(),
//Description
sheetDatabase.getRange(cellSiteDescription[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellProjDescription[1]+rowIndex).getValue(),
//Site Information
sheetDatabase.getRange(cellSiteVisit[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellTeamsLink[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellGoogleMap[1]+rowIndex).getValue(),
//Zoning
sheetDatabase.getRange(cellZoningClass[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellZoningDesc[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellEntitlements[1]+rowIndex).getValue(),
//Construction
sheetDatabase.getRange(cellParkingType[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellConstType[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellUnitSizeRange[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellTotalSqFt[1]+rowIndex).getValue(),
//Financial
sheetDatabase.getRange(cellTotalCost[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellTotalCostDoor[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellTotalCostSF[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellTotalDebt[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellTotalEquity[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellFDGEquity[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellCapitalStructure[1]+rowIndex).getValue(),
//EARLY
sheetDatabase.getRange(cellDateProcessed[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellMarketStudyLink[1]+rowIndex).getValue(),
//Site Photo
//sheetDatabase.getRange(cellPhoto[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellPhotoLibrary[1]+rowIndex).getValue(),
//Stakeholders
sheetDatabase.getRange(cellCoDev[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellFeeDev[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellOperator[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellPartner[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellArchitect[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellEngineer[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellContractor[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellBroker[1]+rowIndex).getValue(),
//Schedule
sheetDatabase.getRange(cellRezoning[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellSitePlan[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellPlanPermit[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellClosing[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellExtensions[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellTimeToClose[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellEstConstStart[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellEstConstLength[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellEstConstFinish[1]+rowIndex).getValue(),
//Additional Notes
sheetDatabase.getRange(cellNotes[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellDropped[1]+rowIndex).getValue(),
//Data Stamps
sheetDatabase.getRange(cellInputDate[1]+rowIndex).getValue(),
sheetDatabase.getRange(cellUpdateDate[1]+rowIndex).getValue()
];
for (var i=0; i<rangesToSetValues.length; i++) {
sheetData.getRange(rangesToSetValues[i]).setValue(values[i]);
}
}
function findInRow(id) {
var sheetDatabase = SpreadsheetApp.getActive().getSheetByName('Data');
var rows = sheetDatabase.getRange("A2:A").getValues();
for (var r=0; r<rows.length; r++) {
if ( rows[r][0] === id ) {
return r+2;
}
}
return -1;
}
function increaseId(id) {
if(id>0) {
var returnId;
returnId = id + 1;
return returnId;
}
}
function findMaxId(myvalues){
return Math.max.apply(null, myvalues);
}
function onOpen(e) {
//initialization
/*var sheet = SpreadsheetApp.getActive().getSheetByName('Form'); //var sheet = SpreadsheetApp.getActive().getSheetByName('Reception');
sheet.getRange(cellId[0]).setValue(1000);
clear_screen();*/
}
function onEdit(e) {
//e.source.setActiveSelection(e.range.offset(2, 0));
var sheet = SpreadsheetApp.getActive().getSheetByName('Form');
var rangeRowId = sheet.getActiveCell().getRow();
var rangeColId = sheet.getActiveCell().getColumn();
//Browser.msgBox("Row Id: " + rangeRowId + " Column Id: " + rangeColId );
if(rangeRowId == 3 && rangeColId == 10) {
var myId = sheet.getRange(cellId[0]).getValue();
lookUpId(myId);
}
}
function clear_screen() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Form');
var rangesToClear = [cellStreetAddress[0], cellCity[0], cellState[0], cellZip[0], cellCounty[0], cellfiftyfive[0], cellIL[0],
cellAL[0], cellMC[0], cellBeds[0], cellPhase[0], cellLand[0], cellBuildable[0],
cellPrice[0], cellGrade[0], cellSource[0], cellClassification[0], cellSiteDescription[0], cellProjDescription[0],
cellSiteVisit[0], cellTeamsLink[0], cellGoogleMap[0], cellZoningClass[0], cellZoningDesc[0], cellEntitlements[0],
cellParkingType[0], cellConstType[0], cellUnitSizeRange[0], cellTotalSqFt[0], cellTotalCost[0], cellTotalCostDoor[0], cellTotalCostSF[0],
cellTotalDebt[0], cellTotalEquity[0], cellFDGEquity[0], cellCapitalStructure[0], cellDateProcessed[0], cellMarketStudyLink[0], cellPhotoLibrary[0], cellCoDev[0],
cellFeeDev[0], cellOperator[0], cellPartner[0], cellArchitect[0], cellEngineer[0], cellContractor[0], cellBroker[0], cellRezoning[0],
cellSitePlan[0], cellPlanPermit[0], cellClosing[0], cellExtensions[0], cellTimeToClose[0], cellEstConstStart[0], cellEstConstLength[0],
cellEstConstFinish[0], cellNotes[0], cellDropped[0], cellInputDate[0], cellUpdateDate[0]];
for (var i=0; i<rangesToClear.length; i++) {
sheet.getRange(rangesToClear[i]).clearContent();
}
var myCellStreetAddress = sheet.getRange(cellStreetAddress[0]);
myCellStreetAddress.activateAsCurrentCell();
}
function lookUpId(id) {
if (id==1000) { //Data Centre mode
clear_screen();
}
if (id>1000) { //lookup mode
getMember(id);
}
}
'''

Multiple Issues with Google Sheets Scripts (JavaScript)

NEEDLES TO SAY, I am very new to this, but trying my hardest to figure all this out. I have a script right now which actively removes all empty rows from an entire workbook. But I'm hoping someone can assist is narrowing this down to a single sheet within that workbook.
function removeEmptyRows() {
SpreadsheetApp.getActive()
.getSheets()
.forEach(function (s) {
c = 0;
s.getRange(1, 1, s.getMaxRows(), s.getMaxColumns())
.getValues()
.forEach(function (r, j) {
if (r.toString()
.replace(/,/g, "")
.length == 0) {
s.deleteRow((j += 1) - c)
c += 1;
}
})
})
}
Ideally I would like for this to remove only blank rows on one sheet within my workbook called 'Racing Results'. The reason I'm need this, is due to how the spreadsheet is setup and having multiple rows merged together. So when I copy these results over to a different sheet, there are gaps between them and I'd like them removed. Here is the script I'm using to copy the data to another sheet.
function Copy() {
var sss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ss = sss.getSheetByName('Score Card');
var range = ss.getRange('A32:E36');
var data = range.getValues();
var tss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ts = tss.getSheetByName('Race Results');
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
If not to throw one final monkey wrench into this colossal mess, I have been attempting to copy two different cell ranges within the same script and I feel like this isn't possible because only the latter one gets copied and the initial one is discarded. Here is the other copy script I am using which runs before the one above this.
function Copy() {
var sss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ss = sss.getSheetByName('Score Card');
var range = ss.getRange('A1:A1');
var data = range.getValues();
var tss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ts = tss.getSheetByName('Race Results');
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
Score Card Screenshot
In a perfect world, what I'm trying to get setup is the following. Above is a screenshot of the scorecard we are using. I am wanting to copy the Current Date (A1) to the 'Racing Results' sheet, then I want to copy the final score with the team names (A32:E36) and move them to the 'Racing Results' sheet right under it. Once that has been done, I want to remove the empty rows between the results because as of right now this is how it looks when copying over. (see image below)
Race Results Screenshot
Thanks in advance to anyone who is able to assist with any of this in any way shape or form.
Edit:
Removing Empty Rows from a spreadsheet is functioning as intended. Still having issues with Copying multiple times in the same action even when giving them different names. Here is my updated script.
function CopyDate() {
var sss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ss = sss.getSheetByName('Score Card');
var range = ss.getRange('A1:A1');//This range is only one cell
var data = range.getValues();
var tss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ts = tss.getSheetByName('Race Results');
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
function CopyScore() {
var sss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ss = sss.getSheetByName('Score Card');
var range = ss.getRange('A32:E36');
var data = range.getValues();
var tss = SpreadsheetApp.openById('18cl69Id4saI455wk__-PhvfxXZa7iWlQpoiRKqBz6bU');
var ts = tss.getSheetByName('Race Results');
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
function delBlankRows(shtname){
var shtname=shtname || 'Race Results';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName(shtname);
var rg=sh.getRange(1,1,sh.getMaxRows(),sh.getLastColumn());
var vA=rg.getValues();
var n=0;
for(var i=0;i<vA.length;i++){
if(!vA[i].join("")){
sh.deleteRow(i-n+1);
n++;
}
}
}
function Sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
function SaveRaceResults() {
CopyDate();
Sleep(5000);
CopyScore();
Sleep(5000);
delBlankRows();
}
Deleting All Blank Rows on a Sheet
function delBlankRows(shtname){
var shtname=shtname || 'Sheet1';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName(shtname);
var rg=sh.getRange(1,1,sh.getMaxRows(),sh.getLastColumn());
var vA=rg.getValues();
var n=0;
for(var i=0;i<vA.length;i++){
if(!vA[i].join("")){
sh.deleteRow(i-n+1);
n++;
}
}
}
Copy function:
function Copy() {
var sss = SpreadsheetApp.getActive();
var ss1 = sss.getSheetByName('Score Card');
var range1 = ss1.getRange('A1');//This range is only one cell
var data1 = range1.getValues();
var ts1 = sss.getSheetByName('Race Results');
ts1.getRange(ts1.getLastRow()+1, 1, data1.length, data1[0].length).setValues(data1);
var ss2 = sss.getSheetByName('Score Card');
var range2 = ss2.getRange('A32:E36');
var data2 = range2.getValues();
var ts2 = sss.getSheetByName('Race Results');
ts2.getRange(ts2.getLastRow()+1, 1, data2.length, data2[0].length).setValues(data2);
}

Increase my script performance Google Sheets Script

I created a function that whenever I run the AppendRow script every row that does not have a dot (".") at the AY column, an array with every information/column that I want from that sheet will be transfered to my main sheet that has around 13k rows atm.
Usually about 20-40 rows get pasted into the first sheet everyday and this script automatically re-arranges the columns to my main sheet.
PROBLEM:
The problem is that each row that gets appended to my main sheet takes around 8-15sec to get transfered and sometimes I get this "exceeded-maximum-execution-time" error thats its really annoying.
Is there any way to make my code run faster or maybe just a way to make the script run more than 5 minutes?
function AppendRow() {
var app = SpreadsheetApp;
var Sheet = app.getActiveSpreadsheet().getSheetByName("Sheet1");
var ss2017 = SpreadsheetApp.openById("");
var sheet2017 = ss2017.getSheetByName("2017");
var lastSourceRow = Sheet.getLastRow();
var lastSourceCol = Sheet.getLastColumn();
var sourceRange = Sheet.getRange(1, 1, lastSourceRow, lastSourceCol);
var sourceData = sourceRange.getValues();
var lenght = sourceData.length;
//Logger.log(lenght);
var time = new Date();
time = Utilities.formatDate(time, "GMT+01:00", "dd/MM/yy, HH:mm:ss");
for(i=5326;i<=lenght;i++)
//my i=5326 is just cuz I had already
// information on my sheet that was transfered before this script existed
{
var columnAY = Sheet.getRange(i,51).getValue();
var checkReservation = Sheet.getRange(i,2).getValue();
if(columnAY == ".")
{
}
else
{
var B = Sheet.getRange(i,2).getValue();
var F = Sheet.getRange(i,6).getValue();
var E = Sheet.getRange(i,5).getValue();
var K = Sheet.getRange(i,11).getValue();
var L = Sheet.getRange(i,12).getValue();
var V = Sheet.getRange(i,22).getValue();
var O = Sheet.getRange(i,15).getValue();
var P = Sheet.getRange(i,16).getValue();
var Q = Sheet.getRange(i,17).getValue();
var AF = Sheet.getRange(i,32).getValue();
var AG = Sheet.getRange(i,33).getValue();
var N = Sheet.getRange(i,14).getValue();
var AI = Sheet.getRange(i,35).getValue();
var AB = Sheet.getRange(i,28).getValue();
var AC = Sheet.getRange(i,29).getValue();
var array = ["",B,F,E,K,L,"",V,O,P,Q,AF,AG,"","",N,N,AI,"","",AB,AC,"","","","",time];
sheet2017.appendRow(array);
Sheet.getRange(i,51).setValue(".");
}
}
}
p.s.: I also have a menu in my spreadsheet to run the script every time i want to not sure if that is any relevant to the question :P
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('My Menu')
.addItem('Copy to 2017', 'AppendRow')
.addToUi();
Every comment is appreciated !! :)
I must admit to having a bit of a problem figure out how my i's coordinated with your's but I think my columns are correct. So check it out but this should run a bit faster.
function AppendRow()
{
var ss0=SpreadsheetApp.getActive();
var sh0=ss.getSheetByName('Sheet1');
var rg0=sh0.getDataRange();
var vA0=rg0.getValues();
var ss1=SpreadsheetApp.openById(id);
var sh1=ss1.getSheetByName('2017');
var TimeStamp=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd/MM/yy HH:mm:ss");
for(i=5326;i<vA0.length;i++)
{
var columnAY=vA0[i][50];
var checkReservation=vA0[i][1];
if(vA0[i][50]!='.')
{
var B = vA0[i][1];//Sheet.getRange(i,2).getValue();
var F = vA0[i][5];//Sheet.getRange(i,6).getValue();
var E = vA0[i][4];//Sheet.getRange(i,5).getValue();
var K = vA0[i][10];//Sheet.getRange(i,11).getValue();
var L = vA0[i][11];//Sheet.getRange(i,12).getValue();
var V = vA0[i][21];//Sheet.getRange(i,22).getValue();
var O = vA0[i][14];//Sheet.getRange(i,15).getValue();
var P = vA0[i][15];//Sheet.getRange(i,16).getValue();
var Q = vA0[i][16];//Sheet.getRange(i,17).getValue();
var AF = vA0[i][31];//Sheet.getRange(i,32).getValue();
var AG = vA0[i][32];//Sheet.getRange(i,33).getValue();
var N = vA0[i][13];//Sheet.getRange(i,14).getValue();
var AI = vA0[i][34];//Sheet.getRange(i,35).getValue();
var AB = vA0[i][27];//Sheet.getRange(i,28).getValue();
var AC = vA0[i][28];//Sheet.getRange(i,29).getValue();
var array = ["",B,F,E,K,L,"",V,O,P,Q,AF,AG,"","",N,N,AI,"","",AB,AC,"","","","",TimeStamp];
sh1.appendRow(array);
sh0.getRange(i+1,51).setValue(".");
}
}
}

Cannot convert class to object in spreadsheet

I wish to get a couple of properties of all the folders in my GDrive and write these properties to a spreadsheet. Because of the large number of folders (over 300) I have decided to use Paging and Batch processing. This seems to be working but I can't seem to write the Array[][] I've created in the batch processing to the spreadsheet.
I'm am getting the following error when I try to set the values in my spreadsheet:
Cannot convert (class)#3cc8188e to Object[][].
I did not find any listed questions that were similar to my problem.
The last line of the script is highlighted when the error appears. Code is:
function myFunction() {
var folder = DocsList.getFolder('MyFolder');
var subfolders = null;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getRange('a1');
var x = 0;
var pageSize = 250;
var token = null;
var xfolders = new Array(500);
do {
var resultset = folder.getFoldersForPaging(pageSize, token);
subfolders = resultset.getFolders();
token = resultset.getToken();
x = subfolders.length;
for (var a = 0; a < subfolders.length; a++) {
var contents = subfolders[a].getFiles();
xfolders[a] = new Array(6);
if(contents.length>0) {
xfolders[a][0] = subfolders[a].getName();
xfolders[a][1] = subfolders[a].getDateCreated();
xfolders[a][2] = subfolders[a].getLastUpdated();
xfolders[a][3] = contents.length;
xfolders[a][4] = subfolders[a].getSize();
xfolders[a][5] = a;
}
}
} while (subfolders.length > pageSize)
sheet.getRange(1, 1, x, 6).setValues(xfolders);
}
You've started with an array xfolders for which you've initialized a length. There's no need to do this - it's enough to know that it's an array. Later you call getRange() with rows==x, where x = subfolders.length... and if that isn't 500 (the length of xfolders) you will end up with your error. (...because you have undefined array elements.)
What you need to do is make sure that the range you're writing to has the same dimensions as the values you're presenting. One way to do that is to calculate the range dimensions using xfolders itself.
Here's your function, refactored to grow xfolders dynamically, and then to use its dimensions for output to the spreadsheet.
function myFunction() {
var folder = DocsList.getFolder('MyFolder');
var subfolders = null;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getRange('a1');
var pageSize = 250;
var token = null;
var xfolders = [];
do {
var resultset = folder.getFoldersForPaging(pageSize, token);
subfolders = resultset.getFolders();
token = resultset.getToken();
for (var a in subfolders) {
var contents = subfolders[a].getFiles();
xfolders[a] = [];
if(contents.length>0) {
xfolders[a][0] = subfolders[a].getName();
xfolders[a][1] = subfolders[a].getDateCreated();
xfolders[a][2] = subfolders[a].getLastUpdated();
xfolders[a][3] = contents.length;
xfolders[a][4] = subfolders[a].getSize();
xfolders[a][5] = a;
}
}
} while (subfolders.length == pageSize) // Quit when we aren't getting enough folders
sheet.getRange(1, 1, xfolders.length, xfolders[0].length).setValues(xfolders);
}

Resources