Im trying to get the url of a editable google form response to show up in google sheets,but it does not seem to be working.
I have seen Awesome Table and Ruben's example. Based of these 2 links and some others, they seem to be working for single form response sheets,but not multiple.
I tried this code 1st:
var formURL = 'https://docs.google.com/forms/d/__Your ID__/viewform';
var sheetName = '__Response sheet__';
var columnIndex = __column where it appears__;
function getEditResponseUrls() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openByUrl(formURL);
for(var i = 2; i < data.length; i++) {
if (data[i][0] != '' && data[i][columnIndex-1] == '') {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if (formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex).setValue(editResponseUrl);
}
}
}
2nd is:
// Form URL
var formID = '__Your ID__';
// Sheet name used as destination of the form responses
var sheetName = '__Response sheet__'';
/*
* Name of the column to be used to hold the response edit URLs
* It should match exactly the header of the related column,
* otherwise it will do nothing.
*/
var columnName = '__name of column where it appears__' ;
// Responses starting row
var startRow = 2;
function getEditResponseUrls(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var columnIndex = headers[0].indexOf(columnName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openById(formId);
for(var i = startRow-1; i < data.length; i++) {
if(data[i][0] && !data[i][columnIndex]) {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if(formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex+1).setValue(editResponseUrl);
}
}
}
Nothing is showing up, and when i check the logs for triggers, it is all working fine, no failures.I have tried putting the global variables within the function, but no changes.
The following points are taken from the latest version from #Rubén latest version on github. This code is a thing of beauty and a joy to behold. Combined with Rubén's detailed instructions, this answer can be setup and running in less than 5 minutes.
change
var formID = '__Your ID__';
to
var formURL = 'https://docs.google.com/forms/d/ -insert id - /edit';
you get the URL from the Forms Editor page.
change
var columnName = 'Form URL';
to
var sheetName = 'URL'; // Column U
replace getEditResponseUrls entirely
function getEditResponseUrls(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var columnIndex = headers[0].indexOf(columnName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openByUrl(formURL);
for(var i = startRow-1; i < data.length; i++) {
if(data[i][0] != '' && data[i][columnIndex] == '') {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if(formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex+1).setValue(editResponseUrl);
}
}
}
Remember to set the installable trigger.
In the Google Sheet that you have linked from the question above, you have declared two functions with the same name and this could be a reason why the first one may never be executing. I tested this code and it seemed to work.
You may want to replace everything in the file with just this code.
var id = '1SdSPhOwi1dWQzRsNpA9zFL-ODorgohST3TMRJLqz16I';
var sheetName = 'Order Information';
var columnIndex = 21;
function getEditResponseUrls() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openById(id);
for(var i = 2; i < data.length; i++) {
if (data[i][0] != '' && data[i][columnIndex-1] == '') {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if (formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex).setValue(editResponseUrl);
}
}
}
I'm trying to pull data from my YouTube channel, numbers of views, etc, into a Google sheet.
I authorized YouTube APIs, but it cannot retrieve my youtube channel ID.
Here's the error message:
Invalid number of arguments provided. Expected 0-1 only (line 31, file "Code")
line 31 is : var analyticsResponse = YouTubeAnalytics.Reports.query(
I'm using a script found online:
function testingYTpage() {
var url = "https://www.youtube.com/watch?v=ua4QGWmDfB8&list=PLOU2XLYxmsILvfJcIASBDbgfxloFz_XsU&index=7";
var rawData = UrlFetchApp.fetch(url).getContentText();
Logger.log(rawData);
}
//
//
//
function spreadsheetAnalytics() {
// Get the channel ID
var myChannels = YouTube.Channels.list('id', {mine: true});
var channel = myChannels.items[0];
var channelId = channel.id;
// Set the dates for our report
var today = new Date();
var monthAgo12 = new Date();
monthAgo12.setMonth(today.getMonth() - 11);
var todayFormatted = Utilities.formatDate(today, 'UTC', 'yyyy-MM-dd')
var oneMonthAgoFormatted = Utilities.formatDate(monthAgo12, 'UTC', 'yyyy-MM-dd');
// The YouTubeAnalytics.Reports.query() function has four required parameters and one optional
// parameter. The first parameter identifies the channel or content owner for which you are
// retrieving data. The second and third parameters specify the start and end dates for the
// report, respectively. The fourth parameter identifies the metrics that you are retrieving.
// The fifth parameter is an object that contains any additional optional parameters
// (dimensions, filters, sort, etc.) that you want to set.
var analyticsResponse = YouTubeAnalytics.Reports.query(
'channel==' + channelId,
oneMonthAgoFormatted,
todayFormatted,
// dimensions=day metrics=views,estimatedMinutesWatched,averageViewDuration,averageViewPercentage,subscribersGained
'views,estimatedMinutesWatched,averageViewDuration,averageViewPercentage,likes,dislikes,shares',
{
dimensions: 'day',
sort: '-day'
});
// Create a new Spreadsheet with rows and columns corresponding to our dates
var ssName = 'YouTube channel report ' + oneMonthAgoFormatted + ' - ' + todayFormatted;
var numRows = analyticsResponse.rows.length;
var numCols = analyticsResponse.columnHeaders.length;
// Add an extra row for column headers
var ssNew = SpreadsheetApp.create(ssName, numRows + 1, numCols);
// Get the first sheet
var sheet = ssNew.getSheets()[0];
// Get the range for the title columns
// Remember, spreadsheets are 1-indexed, whereas arrays are 0-indexed
var headersRange = sheet.getRange(1, 1, 1, numCols);
var headers = [];
// These column headers will correspond with the metrics requested
// in the initial call: views, likes, dislikes, shares
for(var i in analyticsResponse.columnHeaders) {
var columnHeader = analyticsResponse.columnHeaders[i];
var columnName = columnHeader.name;
headers[i] = columnName;
}
// This takes a 2 dimensional array
headersRange.setValues([headers]);
// Bold and freeze the column names
headersRange.setFontWeight('bold');
sheet.setFrozenRows(1);
// Get the data range and set the values
var dataRange = sheet.getRange(2, 1, numRows, numCols);
dataRange.setValues(analyticsResponse.rows);
// Bold and freeze the dates
var dateHeaders = sheet.getRange(1, 1, numRows, 1);
dateHeaders.setFontWeight('bold');
sheet.setFrozenColumns(1);
// Include the headers in our range. The headers are used
// to label the axes
var range = sheet.getRange(1, 1, numRows, numCols);
var chart = sheet.newChart()
.asColumnChart()
.setStacked()
.addRange(range)
.setPosition(4, 2, 10, 10)
.build();
sheet.insertChart(chart);
}
//
// A Helper function to extract the ID of our video
// It works both on version of links:
// 1. https://www.youtube.com/watch?v=BuHEhmp47VE
// 2. http://youtu.be/BuHEhmp47VE
//
function extractVideoID() {
var curSheet = SpreadsheetApp.getActiveSheet();
var ytLinks = curSheet.getRange("D:D");
var totalRows = ytLinks.getNumRows();
var ytVal = ytLinks.getValues();
// let's run on the rows
for (var i = 1; i <= totalRows - 1; i++) {
var curLink = ytVal[i][0];
if (curLink == "") {
break;
}
var videoID = "";
var inx1 = curLink.indexOf('watch?v=') + 8;
if (inx1 == 7) {
// check if it's the short format: http://youtu.be/75EuHl6CSTo
if (curLink != "" && curLink.indexOf("youtu.be") > 0) {
videoID = curLink.substr(16, curLink.length);
}
}
else {
// we have the link in this format: https://www.youtube.com/watch?v=YIgSucMNFAo
var inx2 = curLink.indexOf("&", inx1);
if (inx2 > inx1) {
videoID = curLink.substr(inx1, inx2-inx1);
} else {
videoID = curLink.substr(inx1, curLink.length);
}
}
curSheet.getRange("E" + (i+1)).setValue(videoID);
}
var htmlMsg = HtmlService
.createHtmlOutput('<h3>Done - Please check the IDs on Column D:D</h3>').setTitle('YT Dashboard Example').setWidth(450).setHeight(300);
SpreadsheetApp.getActiveSpreadsheet().show(htmlMsg);
}
//
// Run on all the rows and according to the video ID fetch the feed
//
function fetchAllData() {
var start = new Date().getTime();
var curSheet = SpreadsheetApp.getActiveSheet();
var ytIds = curSheet.getRange("E:E");
var totalRows = ytIds.getNumRows();
var ytVal = ytIds.getValues();
var errMsg = "<h4>Errors:</h4> <ul>";
// let's run on the rows after the header row
for (var i = 1; i <= totalRows - 1; i++) {
// e.g. for a call: https://gdata.youtube.com/feeds/api/videos/YIgSucMNFAo?v=2&prettyprint=true
if (ytVal[i] == "") {
Logger.log("We stopped at row: " + (i+1));
break;
}
var link = "https://gdata.youtube.com/feeds/api/videos/" + ytVal[i] + "?v=2&prettyprint=true";
try {
fetchYTdata(link, i+1);
}
catch (err) {
errMsg += "<li>Line: " + i + " we could not fetch data for ID: " + ytVal[i] + "</li>";
Logger.log("*** ERR: We have issue with " + ytVal[i] + " On line: " + i);
}
}
if (errMsg.length < 24) {
// we do not have any errors at this run
errMsg += "<li> All good for now </li>";
}
var end = new Date().getTime();
var execTime = (end - start) / 1000;
var htmlApp = HtmlService
.createHtmlOutput('<h2>Done updating!</h2><p>It took us: '+ execTime + 'sec. to update: ' +
(i+1) + ' videos</p>' + errMsg).setTitle('YT Stats').setWidth(450).setHeight(450);
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}
//
// Read YT stats data on our videos and fill the sheet with the data
//
function fetchYTdata(url, curRow) {
//var url = 'https://gdata.youtube.com/feeds/api/videos/Eb7rzMxHyOk?v=2&prettyprint=true';
var rawData = UrlFetchApp.fetch(url).getContentText();
//Logger.log(rawData);
// published <published>2014-05-09T06:22:52.000Z</published>
var inx1 = rawData.indexOf('published>') + 10;
var inx2 = rawData.indexOf("T", inx1);
var publishedDate = rawData.substr(inx1, inx2-inx1);
// viewCount='16592'
var inx1 = rawData.indexOf('viewCount') + 11;
var inx2 = rawData.indexOf("'/>", inx1);
var totalViews = rawData.substr(inx1, inx2-inx1);
// <yt:duration seconds='100'/>
var inx1 = rawData.indexOf('duration seconds') + 18;
var inx2 = rawData.indexOf("'/>", inx1);
var durationSec = rawData.substr(inx1, inx2-inx1);
Logger.log(curRow + ") TotalViews: " + totalViews + " durationSec: " + durationSec);
// update the sheet
var ss = SpreadsheetApp.getActiveSheet();
ss.getRange("C" + curRow).setValue(publishedDate);
ss.getRange("G" + curRow).setValue(totalViews);
ss.getRange("H" + curRow).setValue(durationSec);
}
//
// Our custom menu
//
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{ name : "Update Stats", functionName : "fetchAllData"},
{ name : "Extract Video IDs", functionName : "extractVideoID"}
];
spreadsheet.addMenu("YT Dashboard", entries);
};
non-scripted non-API solution (for meanwhile):
CHANNEL ID:
=ARRAYFORMULA(REGEXREPLACE(QUERY(SUBSTITUTE(ARRAY_CONSTRAIN(
IMPORTDATA(https://www.youtube.com/watch?v=rckrnYw5sOA), 3000, 1), """", ""),
"where Col1 contains '<meta itemprop=channelId content='"),
"<meta itemprop=channelId content=|>", ""))
VIEWS:
=VALUE(REGEXREPLACE(TEXT(IMPORTXML("https://www.youtube.com/watch?v=MkgR0SxmMKo",
"//*[contains(#class, 'watch-view-count')]"),0)," view(s)?",""))
MORE: https://stackoverflow.com/a/55064665/5632629
I also faced the same issue. Used the below syntax for the Reports.query method and got it worked.
YouTubeAnalytics.Reports.query({
ids: 'channel==' + channelId,
startDate: formatDateString(lastMonth),
endDate: formatDateString(today),
metrics: metrics.join(','),
dimensions: 'day',
sort: 'day'
});
I have an automated time stamp script that works when clicking a checkbox, a time stamp is populated in the cell next to it. How do I make my script work on certain rows?
Example: Column C has check boxes, Column D is populated with dates when Check boxes are marked.
What I want my TimeStamp script to do:
Work only on Row 11, all the way down.
var COLUMNTOCHECK = 3;
var DATETIMELOCATION = [0, 1];
var SHEETNAME = 'Training Dash'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
if (selectedCell.getColumn() >= COLUMNTOCHECK && selectedCell.getColumn() % 2 == 1) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}
if I understood correctly, this should help.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
if (selectedCell.getColumn() >= COLUMNTOCHECK && selectedCell.getColumn() %
2 == 1 && selectedCell.getRow() > 10) {
var dateTimeCell =
selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
}
}
I have a spreadsheet for project data with time-sheet for each month logged against each project ID
I want to iterate through each sheet and if there is matching project ID , I want to sum up the number of hours logged for each project.
I have written the following code but keep getting the
TypeError: Cannot read property "0" from undefined. (line 31).
This is my sheet : https://goo.gl/rrsSxI
And this is my Code.
function TotalHours(TaskID) {
var a = SpreadsheetApp.getActiveSpreadsheet().getSheets().length;
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sum = 0;
// var fcol = 0;
for (var i = 1; i <= a; ++i) {
// var sheetname = sheets[i].getName();
//var cell = sheets[i].getActiveCell();
//Set active cell to A1 on each sheet to start looking from there
SpreadsheetApp.setActiveSheet(sheets[i])
//var sheet = sh.getActiveSheet();
var range = sheets[i].getRange("A1");
//* sheets[i].setActiveRange(range);
var data = sheets[i].getDataRange().getValues();
for (var row = 2; row <= data.length; ++row) {
if (data[row][0] == TaskID) {
for (var col = 2; col <= 31; ++col) {
sum += sheets[i].getRange(row, col).getValue();
}
}
}
}
return sum;
}
Can someone help me with what I am doing wrong.
I assume you want to exclude the sheet where the formula is going to be used ("Tracker" ?
See if this works ?
function TotalHours(TaskID) {
var sum = 0,
s = SpreadsheetApp.getActive(),
active = s.getActiveSheet().getName(),
sheets = s.getSheets();
for (var i = 0, slen = sheets.length; i < slen; i++) {
if(sheets[i].getName() != active) {
var sheetVal = sheets[i].getDataRange()
.getValues();
for (var j = 0, vlen = sheetVal.length; j < vlen; j++) {
if (sheetVal[j][0] == TaskID) {
for (var k = 2, rlen = sheetVal[j].length; k < rlen; k++) {
var c = sheetVal[j][k]
sum += c && !isNaN(parseFloat(c)) && isFinite(c)? c : 0; //check if cell holds a number
}
}
}
}
}
return sum;
}
I have a Google Sheet that I would like to automatically stamp the username of the person who last made a change to a specific row. I currently have this code to work as I like to do the same function with the time:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() != 2 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
SpreadsheetApp.getActiveSheet().getRange('G' + row.toString()).setValue(time);
};
};
Assuming this is somewhat close to what I require for the username, I tried using this:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() != 2 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
SpreadsheetApp.getActiveSheet().getRange('H' + row.toString()).setValue(Session.getActiveUser);
};
};
But that spits out some weird command that is clearly broken.
How can I do this?
This is the sheet I am working on: https://docs.google.com/spreadsheets/d/1VifU8AJWuPn53-_JCQKbPTjHxKDArkyG0G7zt1wzmPg/edit?usp=sharing
Is this what you are trying to do?
function onEdit(e) {
var r = e.range;
if( r.getColumn() != 2 ) { //checks the column
var time = new Date();
time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
SpreadsheetApp.getActiveSheet().getRange(r.getRow(),8).setValue(Session.getActiveUser().getEmail());
}
}