Converting Google Sheets (Apps Scripts) to a valid CSV format - javascript

I have the below script which essentially looks at a google sheets tab, which has some data that needs to be uploaded to Google Import API (Google Analytics):
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('UK Only');
var maxRows = ss.getLastRow();
var maxColumns = ss.getLastColumn();
var rows = ss.getRange(1, 1, maxRows, maxColumns).getValues();
var rowsCSV = rows.join("\n")
var blobData = Utilities.newBlob(rowsCSV, "application/octet-stream", "GA import data");
try {
var upload = Analytics.Management.Uploads.uploadData("10131233", "UA-1234576-2", "righfghfghfgT8Dox1nwXDg", blobData);
Logger.log("Test Data Import Successful");
}
catch (e) {
console.error(e);
}
The output is something like the below:
ga:productSku,ga:productName,ga:productBrand,ga:productCategoryHierarchy,ga:dimension25,ga:dimension28,ga:dimension31
456456456,example value wit,h char ,that "breaks" csv,fgjfgjf Tjghjghjg,FP,Women,dasdasd
456456456,example value wit,h char ,that "breaks" csv,123123123,FP,Women,dasdasd
456456456,example value wit,h char ,that "breaks" csv,Rdasdasd,FP,asdasdasd
The above shows some values that will break the CSV format (commas and quotes).
How do I go about properly formatting the CSV so that it will not break on these characters?

Managed to resolve this with the below function, I had to account for quotes and commas within values that Google API doesn't like:
function arrayToCSV (twoDiArray) {
var csvRows = [];
for (var i = 0; i < twoDiArray.length; ++i) {
for (var j = 0; j < twoDiArray[i].length; ++j) {
twoDiArray[i][j] = '\"' + twoDiArray[i][j].replace('"','""') + '\"';
}
csvRows.push(twoDiArray[i].join(','));
}
return csvRows.join('\r\n');
};
Making sure to call it with the below:
var maxRows = ss.getLastRow();
var maxColumns = ss.getLastColumn();
var rows = ss.getRange(1, 1, maxRows, maxColumns).getValues();
var rowsCSV = arrayToCSV(rows);

Related

How to read .xlsx data and push to array in .js script for photoshop

How to read .xlsx data and push to array in .js script for photoshop
defaultkrt0iuh8hlot
I am building some automation for work and have reached my limit of comfortable knowlege in scripting.
I have an .xlsx file where:
column A1 thru A20 represents a design we have created.
column B1 thru B20 contains lists of color background templates for each design in column A:
My Excel Document
I also have a folder with .png files whos file names exactly match the names given in column A.
My script uses photoshop batching to open one file (eg: XY-111.png) at a time in that folder,
place that .png on all of the templates (eg: Temp1, Temp3, Temp5) and save each as a .jpg (using a different script).
Screenshot of VS Code
The above code WORKS, but instead of
excelArray.push(Temp1, Temp2, Temp3)
I want to push the data in column B to the array ONLY if app.documents[0] matches column A.
//////////////////////////////////////////////////////////////////////////////////////////////////
The code:
#target photoshop
var Temp1 = "/Temp1.psd"
var Temp2 = "/Temp2.psd"
var Temp3 = "/Temp3.psd"
var Temp4 = "/Temp4.psd"
var Temp5 = "/Temp5.psd"
var Temp6 = "/Temp6.psd"
var Temp7 = "/Temp7.psd"
var Temp8 = "/Temp8.psd"
var Temp9 = "/Temp9.psd"
var Temp10 = "/Temp10.psd"
var picForSim = app.documents[0];
var excelArray = [];
excelArray.push(Temp1, Temp2, Temp3);
for (var i = 0; i < excelArray.length; i++){
if (picForSim.name.charAt(0) === "W"){
//alert(excelArray[i])
app.open(new File(excelArray[i]));
app.doAction(("WGARMENTS-ArtPlace"), ("ProductActions.ATN"))
} else {
//alert(excelArray[i])
app.open(new File(excelArray[i]));
app.doAction(("GARMENTS-ArtPlace"), ("ProductActions.ATN"))
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I took a node.js tutorial and I feel as though I understand the concept of getting data from excel, but I'm having some trouble connecting the dots and getting the info into my script.
any help would be appreciated!
If your.xlxs file is exported as a .csv it'll look like this:
WXY-111,"Temp1, Temp2, Temp3, Temp4"
WXY-222,"Temp2, Temp3, Temp4, Temp5"
WXY-333,"Temp3, Temp4, Temp5, Temp6"
YX-111,"Temp4, Temp5, Temp6, Temp7"
XY-222,"Temp5, Temp6, Temp7, Temp8"
XY-333,"Temp6, Temp7, Temp8, Temp9"
Your can read the text file into Photoshop like this
// Reference to the csv file
var csvFile = "D:\\temp\\mycsv.csv"; // change this!
// automatically read in first file
var theFile = new File(csvFile);
//read in file
var csvArr = []; // array to store CSV
var l = 0;
var csvFile = new File(theFile);
csvFile.open('r');
while(!csvFile.eof)
{
var line = csvFile.readln();
if (line != null && line.length >0)
{
csvArr[l++] = line;
}
}
csvFile.close();
// create an array so we can use the data
var textArr = [];
var msg = "";
for (var i = 0; i< csvArr.length; i++)
{
var line = csvArr[i];
var aStr = line.slice(0, line.indexOf(","));
var bStr = line.slice(line.indexOf(",") + 1);
// replace quotes with nothing
bStr = bStr.replace(/\"/gm, "");
// split column b into seperate elements
var sp = bStr.split(",");
textArr.push([aStr, sp[0], sp[1], sp[2], sp[3]]);
}
// let's loop over the array and see what it says...
for (var i = 0; i< textArr.length; i++)
{
for (var j = 0; j< textArr[i].length; j++)
{
msg += textArr[i][j] + " , ";
}
msg += "\n";
}
alert(msg);
// you can access the info like this:
var data = textArr[4][1];
alert(data); // temp 5

Pulling data from an API using Google App Script

I'm hoping someone could help as I've spent the last day trying to figure out where I'm going wrong.. to no avail.
I'm trying to pull some basic information from https://nvd.nist.gov/ using their API (https://services.nvd.nist.gov/rest/json/cve/1.0/cve-id). I need to pull the data for cve-id's. cve-id's are in column A.
Example cve-id: https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2019-9763
The only information I want is the description, cvssV3baseScore, cvss3vectorString. Very basic you might think but I'm lost after doing tons of research.
I've tried doing this using the following code in Google Apps Script:
function fetchData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("CVEs");
var sheetData = sheet.getDataRange().getValues();
var i, len = sheetData.length, row = [];
for (i = 1; i < len; i++) {
if (sheetData[i][0] == "" || sheetData[i][15] != "")
continue;
// sheetData[i][8] - here 8 represents column I as column A = 0. Column 23 = x, this is where sheet ends (at new Date())
let url = 'https://services.nvd.nist.gov/rest/json/cve/1.0/' + sheetData[i][0];
try {
var id = json.result.CVE_items.cve.CVE_data_meta.ID;
var idStr = '';
var response = UrlFetchApp.fetch(url).getContentText();
row.push([
idStr,
json.result.CVE_items.cve.CVE_data_meta.ID,
json.result.CVE_items.cve.description.description_data.value,
json.result.CVE_Items.impact.baseMetricV3.cvssV3.vectorString,
json.result.CVE_Items.impact.baseMetricV3.cvssV3.baseScore,
]);
//Here (middle number) 10 number denotes the exact column number from where we need to write data. 10 = J
sheet.getRange(i + 1, 2, 1, row[0].length).setValues(row);
}
catch (e) {
continue;
}
}
}
function lookupByNthValue(search_key, sourceColumn, targetColumn, n) {
if(arguments.length < 4){
throw new Error( "Only " + arguments.length + " arguments provided. Requires 4." );
}
var count = 1;
for(var i = 0; i < sourceColumn.length; i++){
if(sourceColumn[i] != search_key){
continue;
}
if(count == n){
return targetColumn[i];
}
count++;
}
}
function onOpen() {
SpreadsheetApp.getUi()
.createMenu("Actions")
.addItem("Fetch Data", 'fetchData')
.addToUi();
}
I would much appreciate if someone could help, link to the Google sheet: https://docs.google.com/spreadsheets/d/18ilCE1udUM87c4YtxXAB52Fm3mEzDk8UggcH96S3JLA/edit?usp=sharing
Thank you in advance.
There are a few problems: len equals 1000 because you have data on row 1000! therefore the script will be too long. You write CVE_items instead of CVE_Items with a capital letter! CVE_Items is not a parent, it is an array, even with a single occurrence. You never parse the response of url fetch. Try this
function fetchData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("CVEs");
var sheetData = sheet.getDataRange().getValues();
var i, len = sheet.getLastRow(), row = [];
Logger.log(len)
for (i = 1; i < len; i++) {
try{
let url = 'https://services.nvd.nist.gov/rest/json/cve/1.0/' + sheetData[i][0];
var json = JSON.parse(UrlFetchApp.fetch(url).getContentText())
var row=[]
row.push([
json.result.CVE_Items[0].cve.CVE_data_meta.ID,
json.result.CVE_Items[0].cve.CVE_data_meta.ID,
json.result.CVE_Items[0].cve.description.description_data.value,
json.result.CVE_Items[0].impact.baseMetricV3.cvssV3.vectorString,
json.result.CVE_Items[0].impact.baseMetricV3.cvssV3.baseScore,
]);
Logger.log(row)
sheet.getRange(i + 1, 2, 1, row[0].length).setValues(row);
}catch(e){}
}
}

ReDash to Google Sheets using Google Script - How do I add the date at the end?

I'm trying to use the following Google script to add data from Redash, then add the current date at the end. Where could I add a var now = new Date(); to input the date on the last row?
The desired outcome is the following. A-C is the data I'm successfully already pulling from Redash, but I want to add the current date. The date will always be in F if that helps.
https://www.dropbox.com/s/lfu9jk06j0fduo8/Screenshot%202020-05-20%2018.44.26.png?dl=0
Thank you for your help!
//FUNCTION FOR GETTING DATA FROM REDASH QUERIES
function getapidata(response, sheetname) {
var array1 = response.getContentText(); //Store data from redash into this variable
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var iLen = array1.length;
var i = 0;
var stringvalue1 = "";
var s1 = "";
var num1 = 1;
var num2 = 1;
var noheader = 1;
for (i = 0; i < iLen; i++) {
var j = i;
do {
s1 = array1[j];
if (s1 == ",") {
if (noheader == 1)
noheader = 1;
else
sheet.getRange(sheet.getLastRow() + num1, num2).setValue(stringvalue1);
num1 = 0;
num2++;
stringvalue1 = "";
} else
stringvalue1 = stringvalue1 + s1;
j++;
} while (s1 != "\n")
if (noheader == 1)
noheader = 1;
else
sheet.getRange(sheet.getLastRow() + num1, num2).setValue(stringvalue1);
noheader = 2;
num1 = 1;
num2 = 1;
stringvalue1 = "";
i = j - 1;
}
}
function getfromRedash() {
//Current SHR (Max Preps)
var redashapi = "API"; //Storing Redash API Value.
var sheetname = "SHEETTITLE"; //Assign your sheetname(where you would want data to be imported) to this variable
var response = UrlFetchApp.fetch(redashapi, {
muteHttpExceptions: true
}); //Storing Redash Cached Query result in this variable.
Logger.log(response.getContentText()); //If any error, error to be logged
getapidata(response, sheetname); //Call function for writing data in google sheets
//The 5 lines of code above can be repeated for multiple sheets in a single google spreadsheet file. For. Eg. If you have 5 different sheets in a single google worksheet, you can just copy paste the above lines of code, and just change the variable “redashapi” so as to make calls to the appropriate redash queries.
}
//THIS FUNCTION IS TO MAKE A NEW MENU IN GOOGLE SHEETS
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('MENU')
.addItem('Get Data from Redash', 'getfromRedash')
.addToUi();
}
Well, you row number addressing is a bit confusing, cause sheet.getLastRow() would change every time you add values after the last row, but you can write something like
sheet.getRange(sheet.getLastRow() + num1, 6).setValue(new Date());
to insert date. You can refer other answers, like
How to format a JavaScript date
for date formatting.

Javascript: Grouping csv data by month instead of day before inserting to Google Sheets

I'm very new to Javascript but have recently started modifying scripts to let us pull various csv reports into Google Sheets, using Google Apps Script. The current script we have for this reads the csv file and then inserts all data after the 8th row (since the rows before that just contain info such as when it was generated, report name etc.).
The challenge now is that we need a year-to-date report that is split by month into Sheets (if we split by day we reach the 2 million cell limit). But as the reporting tool in this case doesn't allow to segment by any other date variable than day, we have to do the split within the script. So we would basically have a YTD report sent with ad-level data split by day. This ad-level data is then grouped by month rather than day within the script, before the data is inserted into Sheets. In other words, I want e.g. cost, impression and click data for an ad to be summed for all daily occurrences within one month.
I've looked around intensively the last couple of days but haven't found a working solution for this specific problem yet. I would be super grateful if someone wanted to take a look at this.
For your reference, please find two scripts below. The first one is the working script we currently have, which just insert the csv data from the 8th row (but no grouping):
function importData() {
var sheet_atlas = SpreadsheetApp.getActive().getSheetByName('sheet_name');
sheet_atlas.getRange('A2:V10000').clearContent();
var sheetName = "sheet_name"
var threads = GmailApp.search("attachment_name")
var msgs = GmailApp.getMessagesForThreads(threads);
var newData = [];
for (var i = 0 ; i < msgs.length; i++) {
for (var j = 0; j < msgs[i].length; j++) {
var attachments = msgs[i][j].getAttachments();
var bodyEmail = msgs[0][0].getBody();
var regExp = new RegExp('a href="(.*?)"', "gi");
//var regExp = new RegExp('data-saferedirecturl="(.*?)"', "gi"); // "i" is for case insensitive
var url = regExp.exec(bodyEmail)[1];
Logger.log(url)
var decode = new XML('<d>' + url + '</d>');
var strDecoded = decode.toString()
var response = UrlFetchApp.fetch(strDecoded).getContentText();
var csvdata = Utilities.parseCsv(response)
var newOrders = []
//Logger.log(csvdata)
for (var eachRow in csvdata){
//for(var col1 in csvdata[8]){
//if(csvdata[8][col1] == 'Campaign Name'){ var campaignCol = col1 }
//else if(csvdata[8][col1] == 'Publisher Name'){ var publisherCol = col1 }
//else if(csvdata[8][col1] == 'Statistics Date'){ var dateCol = col1 }
//}
//Logger.log(dateCol)
//Logger.log(publisherCol)
//Logger.log(campaignCol)
if (eachRow>8)
{
var theRow = []
theRow.push(csvdata[eachRow][0])
theRow.push(csvdata[eachRow][1])
theRow.push(csvdata[eachRow][2])
theRow.push(csvdata[eachRow][3])
theRow.push(csvdata[eachRow][4])
theRow.push(csvdata[eachRow][5])
theRow.push(csvdata[eachRow][6])
theRow.push(csvdata[eachRow][7])
theRow.push(csvdata[eachRow][8])
theRow.push(csvdata[eachRow][9])
theRow.push(csvdata[eachRow][10])
theRow.push(csvdata[eachRow][11])
theRow.push(csvdata[eachRow][12])
theRow.push(csvdata[eachRow][13])
theRow.push(csvdata[eachRow][14])
theRow.push(csvdata[eachRow][15])
theRow.push(csvdata[eachRow][16])
theRow.push(csvdata[eachRow][17])
theRow.push(csvdata[eachRow][18])
theRow.push(csvdata[eachRow][19])
theRow.push(csvdata[eachRow][20])
theRow.push(csvdata[eachRow][21])
newOrders.push(theRow)
}
}
}
}
Logger.log(newOrders)
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(2,1, newOrders.length, newOrders[0].length).setValues(newOrders)
}
The second one is my current attempt at creating the grouping. So far I've managed to break out the month, but when it comes to the summing part it goes all wrong.
function importData() {
var sheet_atlas = SpreadsheetApp.getActive().getSheetByName('sheet name');
sheet_atlas.getRange('L2:AA60000').clearContent();
var sheetName = "sheet name"
var threads = GmailApp.search("attachment_name")
var msgs = GmailApp.getMessagesForThreads(threads);
var newData = [];
for (var i = 0 ; i < msgs.length; i++) {
for (var j = 0; j < msgs[i].length; j++) {
var attachments = msgs[i][j].getAttachments();
var bodyEmail = msgs[0][0].getBody();
var regExp = new RegExp('a href="(.*?)"', "gi");
var url = regExp.exec(bodyEmail)[1];
Logger.log(url)
var decode = new XML('<d>' + url + '</d>');
var strDecoded = decode.toString()
var response = UrlFetchApp.fetch(strDecoded).getContentText();
var csvdata = Utilities.parseCsv(response)
var newOrders = []
//Logger.log(csvdata)
var currMonth = "";
var theRow = [];
var columns = 11;
var formattedMonth = "";
for (var eachRow in csvdata){
//for(var col1 in csvdata[8]){
//if(csvdata[8][col1] == 'Campaign Name'){ var campaignCol = col1 }
//else if(csvdata[8][col1] == 'Publisher Name'){ var publisherCol = col1 }
//else if(csvdata[8][col1] == 'Statistics Date'){ var dateCol = col1 }
//}
//Logger.log(dateCol)
//Logger.log(publisherCol)
//Logger.log(campaignCol)
if (eachRow>8)
{
var date = csvdata[eachRow][2].split("-")
var month = date[0]
if((currMonth != "") && (month.localeCompare(currMonth) != 0)) {
theRow[2] = formattedMonth
newOrders.push(theRow)
theRow = []
currMonth = month
for (var ci = 0; ci < columns; ci++){
// Reset data structure
theRow.push("0")
}
}
formattedMonth = date[0] + "-" + date[2]
for(var cy = 0; cy < columns; cy++) {
if(cy != 2){
// Sum the columns
theRow[cy] = parseFloat(theRow[cy]) + parseFloat(csvdata[eachRow][cy])
}
}
}
}
// Pushing final row
Logger.log(formattedMonth)
newOrders.push(theRow)
}
Logger.log(newOrders)
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(2,12, newOrders.length, newOrders[0].length).setValues(newOrders)
}
}
Thanks in advance and let me know if anything needs to be clarified!
Not an answer as you haven't provided enough information, but some hints on where things might be going wrong.
You really should use semi-colons to end expressions, whoever left you this code to maintain needs to be reminded of their obligations to others.
One issue in your code is that currMonth is an empty string:
var currMonth = "";
...
if (eachRow > 8)
{
var date = csvdata[eachRow][2].split("-")
var month = date[0]
// This expression must always return false since currMonth == ""
if((currMonth != "") && (month.localeCompare(currMonth) != 0)) {
theRow[2] = formattedMonth
newOrders.push(theRow)
theRow = []
// this statement expression will never be executed,
// so currMonth remains an empty string
currMonth = month
for (var ci = 0; ci < columns; ci++){
// Reset data structure
theRow.push("0")
}
}
Also looking at:
for (var eachRow in csvdata){
if (eachRow>8)
presumably eachRow is a string, comparing it to a number using > will always return false if Number(eachRow) does not return a number (e.g. it will evaluate correctly if eachRow is a string like "12" or "6" but not a string like "2017-08-28").
You need to supply a sample for csvdata and the output you expect from processing it.

Pass Function result as an element of an array within a For Loop

I'm having a really hard time adding this small function to my work here:
Basically if data[i][49] is blank I want to assign my name "Clay" to it and otherwise data[i][49]. I've tried putting it in multiple places and it just inserts the whole function body into my spreadsheet.
Thanks for any help!
var whywontthiswork = (function {if(data[i][49] == " "){
"Clay"
}else{
data[i][49]
};})
It keeps saying "Invalid property ID. (line 27, file "C1: Push Load Details")"
function pushLoadDetails() {
deleteCSV();
//allPlainTextFormat(); adds 20 seconds
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName("Loads"), sh1 = ss.getSheetByName("DAT"), sh2 = ss.getSheetByName("Truckstop"),
sh3 = ss.getSheetByName("Posteverywhere"),sh4 = ss.getSheetByName("Push"), sh5 = ss.getSheetByName("Carriers");
// get data from Loads sheet
var data = sh0.getRange(8,4,33,55).getValues();
// create array to hold data (imaginary)
var aMain = new Array();
var bMain = new Array();
var myArray = aMain;
// itterate through data and add to array
//for (var i = 0, len = myArray.length; i < len; i++) {}
for(var i=0; i < 31; i++) { //Controls how many rows it goes through for posting // len = data.length; i < len
var whywontthiswork = (function {if(data[i][49] == " "){
"Clay"
}else{
data[i][49]
};})
if(i != ""){
for(var j=25; j<28; j++) { //Controls which date elements to iterate through for posting
for(var k=32; k<37; k++) { //Controls which trailer type elements to iterate through for posting
var aPushArray = [data[i][0],data[i][1],data[i][2],data[i][3],data[i][4],data[i][5],data[i][6],data[i][7],data[i][8],data[i][9],
data[i][10],data[i][11],data[i][12],data[i][13],data[i][14],data[i][15],data[i][16],data[i][17],data[i][18],data[i][19],
data[i][20],data[i][21],data[i][22],data[i][23],data[i][24],data[i][j],//data[i][25],//data[i][26],data[i][27],
data[i][28],data[i][29],
data[i][30],data[i][31],data[i][k],//data[i][32],data[i][33],data[i][34],data[i][35],data[i][36],
data[i][37],data[i][38],data[i][39],
data[i][40],data[i][41],data[i][42],data[i][43],data[i][44],data[i][45],
data[i][46],data[i][47],data[i][48],[whywontthiswork]//data[i][49]
]
//[Utilities.formatDate(new Date(), "GMT+8", "ddMMyyyyHHmmssSSS")] can go over data[i][44] to make consecutive numbers
//var aJK = Logger.log(aPushArray[0].length);
//var aWidth = return aJK;
//ts = aPushArray.split(',');
var aPush = aMain.push(aPushArray); //add more data sets to import more columns of data
};
};
};
};
// function pushCarrierInfo(){
for(var i=0; i < 31; i++) {
if(i != ""){ //Controls how many rows it goes through for posting
//Controls which trailer type elements to iterate through for posting
deleteSheet("Carriers","A2:AZ")
var bPush = bMain.push(
[data[i][49],data[i][46],data[i][48],data[i][47],
data[i][0],data[i][1],data[i][2],data[i][3],data[i][4],data[i][5],data[i][6],data[i][7],data[i][8],//data[i][9],
//data[i][10],data[i][11],data[i][12],data[i][13],data[i][14],data[i][15],data[i][16],data[i][17],data[i][18],
data[i][19],
//data[i][20],data[i][21],data[i][22],data[i][23],data[i][24],data[i][25],data[i][26],data[i][27],
data[i][28],data[i][29],
data[i][30],data[i][31],//data[i][32],data[i][33],data[i][34],data[i][35],data[i][36],
data[i][37],data[i][38],data[i][39],
data[i][40],data[i][41],data[i][42],data[i][43],data[i][44],data[i][45]//,data[i][46],data[i][48],data[i][49],
//data[i][50],data[i][51],data[i][52],data[i][53]
]); //add more data sets to import more columns of data
};
};
// };
//var aWidth = Logger.log(aPushArray[0].length);
//var bWidth = Logger.log(bPush[0].length);
// add array of data to second sheet. final column length must match # of data sets
sh4.getRange(2, 1, aMain.length, 44).setValues(aMain);
sh5.getRange(2, 1, bMain.length, 27).setValues(bMain);
deleteSheet("Posteverywhere","A3:Z")
//allPlainTextFormat(); adds 20 seconds
//var ss = SpreadsheetApp.getActive();
var ssk = SpreadsheetApp.openById("15bdmuKR9aHZyjffpzN0wFqhnJf6klFmni64OnA3HEEo");
var shk = ssk.getSheetByName("Posteverywhere");
var thissheet = ss.getSheetByName("Posteverywhere"); //row, column, numRows, numColumns
//var thissheet = thisSheet.getRange(4,1,50,30);
// get data from Loads sheet
var dataK = shk.getRange(1,1,34,52).getValues();
//create array to hold data (imaginary)
var koturMain = new Array();
// itterate through data and add to array
for(var i=0; i < 31; i++) {
if(i != ""){ //Controls how many rows it goes through for posting
//Controls which trailer type elements to iterate through for posting
var koturPE = koturMain.push([dataK[i][0],dataK[i][1],dataK[i][2],dataK[i][3],dataK[i][4],dataK[i][5],dataK[i][6],dataK[i][7],dataK[i][8],dataK[i][9],
dataK[i][10],dataK[i][11],dataK[i][12],dataK[i][13],dataK[i][14],dataK[i][15],dataK[i][16],dataK[i][17],dataK[i][18],dataK[i][19],
dataK[i][20],dataK[i][21],dataK[i][22],dataK[i][23],dataK[i][24],dataK[i][25],dataK[i][26],dataK[i][27],dataK[i][28],dataK[i][29],
dataK[i][30],//data[i][31],//data[i][32],data[i][33],data[i][34],data[i][35],data[i][36],
//data[i][37],data[i][38],data[i][39],
//data[i][40],data[i][41],data[i][42],data[i][43],data[i][45],data[i][44],//data[i][46],data[i][47],data[i][48],data[i][49]
]); //add more data sets to import more columns of data
};
};
// add array of data to second sheet. final column length must match # of data sets
// thissheet.getRange(thissheet.getLastRow()+1, 1, koturMain.length, 25).setValues(koturMain);
};
After trying a number of things I realized this would get the job done without increasing processing time and having to mess with formatting or inserting any functions.
Using the or "||" symbol is and if/else statement.
[data[i][49]||"Clay"]

Categories

Resources