Here is my code:
function dataValidation()
{
var ss1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("2019년 출결 정보");
var ss2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("원생 정보");
var array = [];
for (var c = 0; c <= 1000; c++)
{
array[c] = [];
}
var count = 0;
var lastRow1;
var data1 = ss1.getRange("C:C").getValues();
for (var k = data1.length-1; k >= 0; k--)
{
if (data1[k][0] != null && data1[k][0] != '')
{
lastRow1 = k+1;
break;
}
}
var lastRow2;
var data2 = ss2.getRange("A:A").getValues();
for (var n = data2.length-1; n >=0; n--)
{
if (data2[n][0] != null && data2[n][0] != '')
{
lastRow2 = n+1;
break;
}
}
for (var i = 2; i <= lastRow1; i++)
{
for (var j = 2; j <= lastRow2; j++)
{
if (ss1.getRange(i, 3).getDisplayValue() == ss2.getRange(j, 1).getDisplayValue())
{
array[i-1][count] = ss2.getRange(j, 2).getDisplayValue();
count++;
}
}
var rule = SpreadsheetApp.newDataValidation().requireValueInList(array[i-1], true).setAllowInvalid(false).build();
ss1.getRange(i, 4).setDataValidation(rule);
count = 0;
}
}
I am so concerned because the compile speed takes too long... It takes about a minute to fully execute :(
Execution hint suggests to change from line 71:
for (var i = 2; i <= lastRow1; i++)
{
for (var j = 2; j <= lastRow2; j++)
{
if (ss1.getRange(i, 3).getDisplayValue() == ss2.getRange(j, 1).getDisplayValue())
{
array[i-1][count] = ss2.getRange(j, 2).getDisplayValue();
count++;
}
}
Personally I think this is because of nested loops but I don't have any clever ideas to replace this loops, nor I have much knowledge about google apps script.
What else I can do to decrease compile time?
Related
I'm attempting to limit the number of columns pulled from a data source. The source has 10 columns, I only need the first 4 columns. Seems I am confusing myself on userData and var j plus var k and var l when attempting to create an HTML table of the output.
When I use var i = 0; i < values.length; i++ and var j = 0; j < header.length; j++ with var k = 0; k < userData.length; k++ and var l = 0; l < userData[0].length; l++ the HTML output returns all the columns.
How can I limit the output to just the first 4 columns?
var sheetID = 'example';
var dataSheet = 'SOP Update ACK';
var emailHeader = 'Email Address';
var activeUser = Session.getActiveUser();
var ss = SpreadsheetApp.openById(sheetID);
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('Index').setTitle('My SOP Updates');
}
function currentUser() {
if (activeUser !== '') {
return activeUser.getEmail();
} else {
return "Couldn't detect user!!!";
}
}
function getData() {
var sheetName = dataSheet;
var activeSheet = ss.getSheetByName(sheetName);
var values = activeSheet.getDataRange().getDisplayValues();
var header = values[0];
var emailIndex = header.indexOf(emailHeader);
var userData = [];
for (var i = 0; i < values.length; i++) {
if (values[i][emailIndex] == activeUser) {
userData.push(values[i]);
}
}
if (userData.length > 0) {
var tableStart = '\n<table class="table table-hover table-sm">';
var tableHead = '\n<thead>\n<tr>';
for (var j = 0; j < header.length; j++) {
tableHead = tableHead + '\n<th>' + header[j] + '</th>';
}
tableHead = tableHead + '\n</tr>\n</thead>';
var tableBody = '\n<tbody>';
for (var k = 0; k < userData.length; k++) {
tableBody = tableBody + '\n<tr>';
for (var l = 0; l < userData[0].length; l++) {
tableBody = tableBody + '\n<td>' + userData[k][l] + '</td>';
}
tableBody = tableBody + '\n</tr>\n';
}
var tableEnd = '</tbody>\n</table>';
var tableHtml = tableStart + tableHead + tableBody + tableEnd;
return tableHtml;
} else {
return '<table class="table"><tbody><tr><td>No data found.</td></tr></tbody></table>';
}
}
I got this to work:
You will have to make some minor modification to get it to work for you because I tested it on my temp data since you did not provide any
function getData() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const [header, ...values] = sh.getDataRange().getDisplayValues();
const emailIndex = header.indexOf('Email Address');
const userData = [];
const activeUser = Session.getActiveUser();
for (let i = 0; i < values.length; i++) {
if (values[i][emailIndex] == activeUser) {
userData.push(values[i].slice(0,4));
}
}
if (userData.length > 0) {
let tableStart = '\n<table class="table table-hover table-sm">';
let tableHead = '\n<thead>\n<tr>';
for (let j = 0; j < header.length; j++) {
tableHead = tableHead + '\n<th>' + header[j] + '</th>';
}
tableHead += '\n</tr>\n</thead>';
let tableBody = '\n<tbody>';
for (let k = 0; k < userData.length; k++) {
tableBody = tableBody + '\n<tr>';
for (let l = 0; l < userData[0].length; l++) {
tableBody = tableBody + '\n<td>' + userData[k][l] + '</td>';
}
tableBody = tableBody + '\n</tr>\n';
}
let tableEnd = '</tbody>\n</table>';
let tableHtml = tableStart + tableHead + tableBody + tableEnd;
return tableHtml;
} else {
return '<table class="table"><tbody><tr><td>No data found.</td></tr></tbody></table>';
}
}
I am having troubles in
Exception: The number of rows in the data does not match the number of rows in the range. The data has 1 but the range has 8. Google Script
function myFunction() {
var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Staging');
var s2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var Avals = s2.getRange("A1:A").getValues();
var Alast = Avals.filter(String).length;
var resultArray = [];
for(var n=0; n < values1.length ; n++)
{
var keep = false;
var counter = 0;
for(var p=0; p < values2.length ; p++)
{
if( values1[n][1] == values2[p][1])
{
keep = true;
break ;
}
}
if(keep == false)
{
resultArray.push([values1[n]]);
//s2.appendRow( values1[n] );
s2.getRange(Alast + 1, 1, values1[n].length, values1[n].length).setValues([values1[n]]);
//s2.getRange(s2.getLastRow() + 1, 1, values1.length, values1[0].length).appendRow( values1[n] );
resultArray = [];
keep = false
}
}
}
I believe you are trying to keep appending a new row to the end of the data on "Final". You should revise you script as shown below. Use resultArray to collect all new rows and save it to spreadsheet once.
function myFunction() {
var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Staging');
var s2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
// s2.getDataRange() will get up to the last row of data
var Alast = values2.length;
var resultArray = [];
for(var n=0; n < values1.length ; n++)
{
var keep = false;
var counter = 0;
for(var p=0; p < values2.length ; p++)
{
if( values1[n][1] == values2[p][1])
{
keep = true;
break ;
}
}
if(keep == false)
{
resultArray.push(values1[n]);
keep = false
}
}
if( resultArray.length > 0 ) {
s2.getRange(Alast+1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
}
i tried to find any info on it, i want to use height of the row in a cycle, i know that there is i.length, but is it one for height?
example with length:
for (var i = 2; i <= data.length+1; ++i)
i want it to look something like:
for (var g = 1; g<=grupa.height+1;g++)
this is my whole code, if you want for some reason to look at it:
function myFunction()
{
const ss = SpreadsheetApp.openByUrl ('my url');
var sheet = ss.getSheetByName("Розклад")
var data = sheet.getRange(2, 1, sheet.getLastRow()-1 , sheet.getLastColumn()).getValues();
Logger.log(data);
for (var i = 2; i <= data.length+1; ++i)
{
var data1 = sheet.getRange(i,1,1,13).getValues()
var data2 = sheet.getRange(i,8,1,13).getValues()
var grupas = data [i-2][4]
var cell= data [i-2][8]
var grupa = ss.getSheetByName(grupas)
var grupav = grupa.getRange(1,1,grupa.getLastRow(), grupa.getLastColumn()).getValues();
for (var g = 1; g<=grupa.height+1;g++)
{
var grupap = grupa[0][g-1]
Logger.log(grupap)
}
Logger.log(grupas)
Logger.log(grupav)
Logger.log(cell)
Logger.log(data1);
if ( cell ==="")
{
Logger.log('blank cell');
}
else
{
Logger.log(data2)
}
Logger.log(i-1);
}
}
var grupa = ss.getSheetByName(grupas)
var grupav = grupa.getRange(1,1,grupa.getLastRow(), grupa.getLastColumn()).getValues();
for (var j = 0; j < grupav.length; j++) {
/* grupav[j][0] */
}
Row loop
const col = 0;
const values = sheet.getDataRange().getValues();
for (const i = 0; i < values.length; i++) {
/* values[i][col] */
}
Column loop
const row = 0;
const values = sheet.getDataRange().getValues();
for (const j = 0; j < values[0].length; j++) {
/* values[row][j] */
}
Row & Column loop
const values = sheet.getDataRange().getValues();
for (const i = 0; i < values.length; i++) {
for (const j = 0; j < values[0].length; j++) {
/* values[i][j] */
}
}
I have a script where I'm syncing my google contacts with google spreadsheet.
I have around 11000 contacts in my account and I've written a function getContacts which fetches all contact information from the google contacts and sends it to a function printContacts,how ever as there are too many contacts the script fails to execute to completion.
I tried using the code to create a trigger everytime the max execution time is reached but now I'm facing a new problem,as I have too many ContactsApi call the script is showing an error saying 'user quota exceeded'
If anyone can get this code to work,it'd be a great help
//Function gets the contacts from the Google Contacts and stores in an array. Calls printContacts function with that array and no. of rows as argument
function getContacts() {
var startTime= (new Date()).getTime();
Logger.log("start time set")
var scriptProperties = PropertiesService.getScriptProperties();
var num_contacts = scriptProperties.getProperty('num_contacts');
Logger.log("script property num_contacts : "+Number(num_contacts))
setHeader();
var groupName = 'System Group: My Contacts';
var group = ContactsApp.getContactGroup(groupName);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var contacts = ContactsApp.getContactsByGroup(group);
var allContacts = [];
var contactDetails = [];
var data, numData, dataArray = [], extraArray = [];
var contact_length = contacts.length;
Logger.log("contact_length : "+contact_length);
Logger.log("contacts.length : "+contacts.length);
var MAX_RUNNING_TIME = 300000;
var REASONALBLE_TIME_TO_WAIT = 60000;
var limit = contact_length;
Logger.log("limit :"+limit)
for(var i=Number(num_contacts); i <= contacts.length; i++) {
var currTime = (new Date()).getTime();
if(currTime - startTime >= MAX_RUNNING_TIME){
Logger.log("Value of i in if section : "+i)
scriptProperties.setProperty("num_contacts",i);
ScriptApp.newTrigger("getContacts")
.timeBased()
.at(new Date(currTime+REASONALBLE_TIME_TO_WAIT))
.create();
Logger.log("trigger created for i = "+i)
break;
}
else{
if(i < contacts.length)
{
allContacts[i] = [];
contactDetails[0] = contacts[i].getGivenName();
contactDetails[1] = contacts[i].getFamilyName();
data = contacts[i].getEmails();
numData = data.length;
for(var j = 0; j < numData; j++) {
dataArray[j] = data[j].getAddress();
}
contactDetails[2] = dataArray.join("\n");
dataArray = [];
data = contacts[i].getPhones(ContactsApp.Field.MOBILE_PHONE);
numData = data.length;
for(var j = 0; j < numData; j++) {
dataArray[j] = data[j].getPhoneNumber();
}
contactDetails[3] = dataArray.join("\n");
dataArray = [];
data = contacts[i].getPhones(ContactsApp.Field.WORK_PHONE);
numData = data.length;
for(var j = 0; j < numData; j++) {
dataArray[j] = data[j].getPhoneNumber();
}
contactDetails[5] = dataArray.join("\n");
dataArray = [];
// new section for home_phone ----- start
data = contacts[i].getPhones(ContactsApp.Field.HOME_PHONE);
numData = data.length;
for(var j = 0; j < numData; j++){
dataArray[j] = data[j].getPhoneNumber();
}
contactDetails[6] = dataArray.join("\n");
dataArray = [];
// new section for home_phone ----- end
data = contacts[i].getCompanies();
numData = data.length;
for(var j = 0; j < numData; j++) {
dataArray[j] = data[j].getCompanyName();
extraArray[j] = data[j].getJobTitle();
}
contactDetails[7] = dataArray.join("\n");
dataArray = [];
contactDetails[8] = extraArray.join("\n");
data = contacts[i].getAddresses();
numData = data.length;
for(var j = 0; j < numData; j++) {
dataArray[j] = data[j].getAddress();
}
contactDetails[9] = dataArray.join("\n");
dataArray = [];
contactDetails[10] = contacts[i].getNotes();
data = contacts[i].getCustomFields();
numData = data.length;
for(var j = 0; j < numData; j++) {
dataArray[j] = data[j].getValue();
}
contactDetails[11] = dataArray.join("\n");
dataArray = [];
data = [];
data = contacts[i].getContactGroups();
numData = data.length;
for(var j = 0; j < numData; j++) {
if(data[j].getName() == 'System Group: My Contacts')
{
continue;
}
else
{
dataArray[j] = data[j].getName();
}
}
contactDetails[4] = dataArray.join("\n");
dataArray = [];
contactDetails[12] = 'saved';
contactDetails[13] = contacts[i].getId();
for(var j = 0; j < 14; j++) {
allContacts[i][j] = contactDetails[j];
}
}
}
}
Logger.log("last value of i "+i);
Logger.log("num of contacts "+allContacts.length)
printContacts(allContacts, contacts.length);
setProperties_getContacts();
return;
}
//Function to print contacts to the spreadsheet.
function printContacts(contactsArray, i) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange(2, 1, i, 14);
range.setValues(contactsArray);
}
I am writing a Google script to copy values from a column of importXML data based on a certain date. Below is where I am currently, but the issue I am running into now is when the script is run it is removing the importXML formulas located in columnns G and H:
function moveValuesOnly2() {
var sheet = SpreadsheetApp.openById("0At2fAZApHI8adHJxWU5EVVBEbHd0YzA5UVBwOGQ3ZHc");
SpreadsheetApp.setActiveSpreadsheet(sheet);
SpreadsheetApp.setActiveSheet(sheet.getSheetByName('Dylan'))
var range= sheet.getRange("B3:AJ50");
var currentValue = range.getValues();
var COL_B = 0; // relative to col B
var COL_G = 5;
var COL_H = 6;
var COL_I = 7;
var COL_J = 8;
var COL_K = 9;
var COL_L = 10;
var COL_N = 12;
var COL_O = 13;
var COL_P = 14;
var COL_R = 16;
var COL_S = 17;
var COL_T = 18;
var COL_V = 20;
var COL_W = 21;
var COL_X = 22;
var COL_Z = 24;
var COL_AA = 25;
var COL_AB = 26;
var COL_AD = 28;
var COL_AE = 29;
var COL_AF = 30;
var COL_AH = 32;
var COL_AI = 33;
var COL_AJ = 34;
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (1)) {
// Copy value at 0 days
currentValue[i][COL_J] = currentValue[i][COL_G];
currentValue[i][COL_K] = currentValue[i][COL_H];
currentValue[i][COL_L] = currentValue[i][COL_I];
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (15)) {
// Copy value at 15 days
currentValue[i][COL_N] = currentValue[i][COL_G];
currentValue[i][COL_O] = currentValue[i][COL_H];
currentValue[i][COL_P] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (30)) {
// Copy value at 30 days
currentValue[i][COL_R] = currentValue[i][COL_G];
currentValue[i][COL_S] = currentValue[i][COL_H];
currentValue[i][COL_T] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (45)) {
// Copy value at 45 days
currentValue[i][COL_V] = currentValue[i][COL_G];
currentValue[i][COL_W] = currentValue[i][COL_H];
currentValue[i][COL_X] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (60)) {
// Copy value at 60 days
currentValue[i][COL_Z] = currentValue[i][COL_G];
currentValue[i][COL_AA] = currentValue[i][COL_H];
currentValue[i][COL_AB] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (75)) {
// Copy value at 75 days
currentValue[i][COL_AD] = currentValue[i][COL_G];
currentValue[i][COL_AE] = currentValue[i][COL_H];
currentValue[i][COL_AF] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (90)) {
// Copy value at 90 days
currentValue[i][COL_AH] = currentValue[i][COL_G];
currentValue[i][COL_AI] = currentValue[i][COL_H];
currentValue[i][COL_AJ] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}}}
Is there any way to prevent the ImportXML functions located in those columns from being lost each time the script is run. Also a huge thank you to user #Srik to getting to me to the point I am at!
I would think that the problem lies from the fact that you setValues on the range which includes your columns G & H.
The solution lies in using
var specialRange = sheet.getRange("G3:H50");
var formulas = specialRange.getFormulas();
at the begining of your script to save the formulas and then after your setValues call to use the following code
specialRange.setFormulas(formulas);
Let me know how that works for you.