Self Populating Cell in Google Sheets after a ZAPIER ZAP is run - javascript

I live oversees (military) and get emails when I have a package that arrives at my Post Office and is ready for pickup. I have a Zapier PARSER email account and associated ZAP setup that pulls the data from the email and updates a Google Sheets document with its shelf location, tracking number, and a few other important things from the email so the clerks can get my stuff quickly. It works great. In that same workbook, I have another sheet that I update using an app on my phone to scan tracking barcodes to when I physically pick up the package. On the first sheet I have a basic VLOOKUP function that looks for the tracking number in the scanned list to mark it off as PICKED UP (I get a lot of packages )
I have to manually go in and drag the function down when the ZAP creates a new row. it's not difficult, but i want to have it all automatic. that's what computers are for! I created a google script from the SCRIPT editor on my sheet to do it (I think)
function onEdit(e)
{
var row = e.range.getRow(); //Determine the Row # Just added
var sheet = e.range.getSheet(); //Determine the Sheet, probably not needed
var row_string = row.toString(); //Convert the Row Number to a string.
var start_string = "=VLOOKUP(B"; //this is a fixed part of the function I need in the E column of this row.
var end_string = ", 'Scanned Packages Fixed'!A:B, 2, FALSE)"; //This is the fixed part of the function at the end of the row.
var set_lookup_cell = start_string.concat(row_string, end_string); //Smash the strings together to build a full function
sheet.getRange(row,5).setValue(set_lookup_cell); //Put the full function of the string into the proper cell (E)
}
I am not 100% up on how this would get called automatically or if I am missing a way to link it to the sheet itself. When I hit "run" on the code I got this:
TypeError: Cannot read property 'range' of undefined (line 3, file "Code")
I went into my sheet and just added something in the first column of a new row to see what happened, nothing did.
Any help would be super appreciated!
EDIT: Basically, I need Column E of the row to say this
=VLOOKUP(B63, 'Scanned Packages Fixed'!A:B, 2, FALSE) [Where 63 is the row#]

Turns out, the code above does work. I am just not patient and didn't let the sheet update and script run automatically after an update to a row.
Turns out, this isn't triggered when the zap populates the sheet... Only if I do.

Related

Problem with request google sheets with Highcharts error 404

I've been having problems with Highcharts when requesting from a google sheet, few days ago it was all good, but suddenly it was throwing an error 404, and I don't know what to do. Higcharts only ask you to put the google spreadsheet key, and the number of the tab, and nothing else. I want to change the URL that Higcharts makes the request but I don't know how.
Error:
data.src.js:124 GET https://spreadsheets.google.com/feeds/cells/1YtSfF3-9kzx1G6zwllLX9GhkVgufSUjQyKVI3Ken-AA/2/public/values?alt=json 404
It seems like a google issue: https://support.google.com/docs/thread/121088347/retrieving-data-from-sheets-results-in-404-error-50-of-the-time
feeds is the older version of Sheets API and it's shut down.
See Google's announcement here https://cloud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api
Try tu use this function to get values as json
function getJson(id,gid){
var txt = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/d/${id}/gviz/tq?tqx=out:json&tq&gid=${gid}`).getContentText();
var jsonString = txt.match(/(?<="table":).*(?=}\);)/g)[0]
var json = JSON.parse(jsonString)
return(json)
}
with id of the spreadsheet and gid of your sheet (may be 0)
note that first row is dedicated to labels, then rows[0] corresponds to row#2 and c[0] corresponds to column A
if you want to retrieve B1 (column 2 row 1) : json.cols[1].label
if you want to retrieve B2 (column 2 row 2) : json.rows[0].c[1].v

Google Sheets "Script Function Could Not Be Found"

First time posting here and a newbie at javascript, so hoping this is a very simple fix. I have created an appendRow script (after following a few different examples and amending them for my use). The intention is to have 4 cells at the top of a Google Sheet that are automatically added to the bottom of data in columns A, B, C & D.
Code:
var headers = ['Today' , 'Month' , 'Total Value' , 'Cash Invested'];
var data1 = ['Today' , 'Month' , 'Total Value' , 'Cash Invested'];
var data = [headers , data1];
function putMultipleValues()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("VG Investment Value");
sheet.appendRow(['=a5','=b5','=c5','=d5']);
}
All is working ok, except that the only way I have found to connect the button to the script is by using the 'Select function' name "putMultipleValues". If I use the name of the script (that I have given it) - "VG Table", a "script could not be found" error shows.
This would not be an issue, except I would like to use an almost identical script on a different, almost identical, sheet. The trouble is that this new script also has the 'Select function' name "putMultipleValues" so both scripts fail.
Does anyone know how to change the button so that it links to the script's name rather than it's function?
Many thanks in advance.
Hi and welcome Markrc !
I don't have much experience in Google Script, but from what I understood, a Spreadsheet (and its sheets) is connected to a Script Project.
This Script Project can contains one or more script files.
Each Script files can one or more functions.
The function is what Google Sheet will call a script. (I know, this is a bit confusing)
You can link a script to a button, wich means you can link a function.
However, if you use global variables in your script, the function can access to it (as you noticed). You can link different buttons to different scripts (functions), but you can not link a button to a Script Project or a Script File.
I am not 100% sur of this, but this is how I think it works. If anyone could confirm it, it would be great.
Hope it helped !

Adwords scripts not writing to google sheet

I recently made a script to get info from the Adwords API report KEYWORDS_PERFORMANCE_REPORT that outputed the information to a google sheet and it worked fine.
I decided to make another report for extensions all i basically did was copy and past the old code changing the URL for the google sheet and which report it should use PLACEHOLDER_REPORT instead of the keyword one, as well as some of the metrics.
The script runs without saying that anything is wrong but it doesn't output the values to where its supposed to, or at all for that matter.
function processAccount()
{
var report = AdWordsApp.report(
"SELECT AccountDescriptiveName, CampaignName, ExtensionPlaceholderCreativeId, ExtensionPlaceholderType " +
"FROM PLACEHOLDER_REPORT " +
"WHERE CampaignStatus = ENABLED " +
"DURING LAST_7_DAYS",
{apiVersion: 'v201705'});
var rows = report.rows();
if (rows.hasNext())
{
writeReport(rows);
}
}
function writeReport(rows)
{
var spreadsheet = validateAndGetSpreadsheet(URL);
// Clear all rows in the Details tab of the spreadsheet below the header row.
var clearRange = spreadsheet.getRangeByName('Headers')
.offset(1, 0, spreadsheet.getSheetByName('Details')
.getDataRange().getLastRow())
.clearContent();
// Build each row of output values in the order of the Report tab columns.
var outputValues = [];
while (rows.hasNext())
{
var row = rows.next();
outputValues.push([
row["AccountDescriptiveName"],
row["CampaignName"],
row["ExtensionPlaceholderCreativeId"],
row["ExtensionPlaceholderType"],
]);
}
// Find the first open row on the Report tab below the headers and create a
// range large enough to hold all of the failures, one per row.
var lastRow = spreadsheet.getSheetByName('Details')
.getDataRange().getLastRow();
var headers = spreadsheet.getRangeByName('Headers');
var outputRange = headers
.offset(lastRow - headers.getRow() + 1, 0, outputValues.length);
Logger.log("outputValue length is "+outputValues.length);
outputRange.setValues(outputValues);
Logger.log(outputValues[0]);
spreadsheet.getRangeByName('Date').setValue(new Date());
}
I know its finding the sheet because it clears the rows below the header like its supposed to and puts the date and time when it is run.
The logger shows that outputValue has a length of 293 so its definitly getting stuff and also logs the stuff at outputValue[0] so i dont know why it wont write to the sheet.
I called the adwords helpline but they said thearen't't trained in the api stuff so they don't know whats up, and i've googled it with no success
Everytime it runs you just want the new values, so I suggest using
spreadsheet.getRange(1, 1, data.length, 4).setValues(outputValues);
Replace your last line with this one, the last arguments is how many columns you will write, so in your case is 4, also change your code's logic so it will write the values in the end, not at every iteration!
Turns out i had messed up the output range it was outputting it about 120 rows lower than it was supposed toi do if id just scrolled down i would of seen it. fixed that and now all is fine.

Google Sheets/JIRA Connection

I'm trying to connect Google Sheets to JIRA to gather the data for updating reports automatically.
I'm struggling however with a couple of points in this modified script.
I want to return the component field but calling the name field returns undefined.
var components = data["issues"][id].fields.components.name;
If I remove the name field, then I get the following response:
{name=#####, self=https://www.#########/rest/api/2/component/26357, id=26357}
The second issue is that only a handful of issues are being rendered. As far as I can see my REST call looks OK, as does the writing to tables:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); //select active spreadsheet
sheet.getRange(2, 1, issuessss.length, 7).setValues(issuessss); // write from cell A2
break;
Anyone have any ideas that could help?
The field component is an array so components.name does not exist (i.e components[0].name would work).
Before setting the values, try to execute a flush first.

HTML Function displaying as plain text

I have been working on this for quite some time, and have basically been teaching myself HTML, so I apologize if the code is sloppy or if this is a simple fix. Here is what I am attempting to do, and the problem I am running into:
Take Google Form responses, generate an email based on those responses and dynamically email a certain person in my organization based on the location response(this part is done and working, just adding for context). Then create a survey response that sends info back to the original responder, sent from the administrator that the form was sent to. This is the js that I have running, that is working when it is ran in the google project:
function getid() {
var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/a/raytownschools.org/spreadsheets/d/1YWHu_yKn5bqq63x1A4e4-vBUtZANj-xjeF07IBpHP64/edit?usp=sharing');
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
var sheet = spreadsheet.getActiveSheet();
var lastRow = sheet.getLastRow();
}
When I attempt to run that in my HTML code, and insert it into the element, it is simply inserting that code as raw text. HTML isn't running the function, or returning the data that it should be (and does return when ran outside the HTML code as a js app).
I can post the full HTML code if that would be helpful. Hopefully someone on here can help me out.
What you have there is a Javascript function. There aren't functions in HTML, HTML is a markup language.
You must add that function inside Javascript tags like this:
<script type="text/javascript">
function getid() {
var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/a/raytownschools.org/spreadsheets/d/1YWHu_yKn5bqq63x1A4e4-vBUtZANj-xjeF07IBpHP64/edit?usp=sharing');
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
var sheet = spreadsheet.getActiveSheet();
var lastRow = sheet.getLastRow();
}
alert( getid() );
</script>
Take a look at here, on how to use javascript.
Edit
Seems like that code you're trying to execute is for Google Apps Script. I think you must execute it inside the Google script editor, because they don't make this API available for regular websites. Here is a running example with your code.

Categories