I'm trying to create a function to pull data from google finance and have it automatically log data to multiple sheets of a document. I can't seem to get it to add a function as a function it always adds it as text until i edit the cell
function addLog() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var numLastRows = rows.getLastRow();
sheet.appendRow(['2012/10/28','=b92','=c92','Log (auto)','','=index(GoogleFinance(C92,"price",A92);2;2)','','=h92','=i92','=j92','=k92']);
};
i couldn't figure out how to make it reference the cell above (row 92 in this case) either :(
You will need to use the setFormula() method rather than appendRow(). Perhaps something like:
function addLog() {
var sheet = SpreadsheetApp.getActiveSheet();
var numLastRow = sheet.getLastRow();
//add a row at end if necessary
if (sheet.getMaxRows() == numLastRow) sheet.insertRowAfter(numLastRow);
sheet.getRange(numLastRow + 1, 2, 1, 10)
.setFormulas([['=b' + numLastRow,
'=c' + numLastRow,
'',
'',
'=index(GoogleFinance(C' + numLastRow + ',"price",A' + numLastRow + ');2;2)',
'',
'=h' + numLastRow,
'=i' + numLastRow,
'=j' + numLastRow,
'=k' + numLastRow]]);
sheet.getRange(numLastRow + 1, 1).setValue('2012/10/28');
sheet.getRange(numLastRow + 1, 4).setValue('Log (auto)');
}
To perform this action from the xth sheet through to the yth sheet (using zero-based index), I think the most efficient way would be:
function addAllLog() {
var sheets = SpreadsheetApp.getActive().getSheets();
var x = 1;
var y = 34;
for (var i = x; i <= y; i++) {
addLog(sheets[i]);
}
}
function addLog(sheet) {
var numLastRow = sheet.getLastRow();
//add a row at end if necessary
if (sheet.getMaxRows() == numLastRow) sheet.insertRowAfter(numLastRow);
sheet.getRange(numLastRow + 1, 2, 1, 10)
.setFormulas([['=b' + numLastRow,
'=c' + numLastRow,
'',
'',
'=index(GoogleFinance(C' + numLastRow + ',"price",A' + numLastRow + ');2;2)',
'',
'=h' + numLastRow,
'=i' + numLastRow,
'=j' + numLastRow,
'=k' + numLastRow]]);
sheet.getRange(numLastRow + 1, 1).setValue('2012/10/28');
sheet.getRange(numLastRow + 1, 4).setValue('Log (auto)');
}
Related
I'm trying to insert a timestamp into a cell in the "timestamp" column with the same row index as the edited or pasted value in the "status" column, only insert a timestamp in to an empty cell in "timestamp" column, while skipping the cell that already has a value in the "timestamp" column.
I'd also like to convert the date to a number, such as yymmddHHmmss*1000 + seri number column "No." Exp: If the timestamp is 22:01:20 14:08:05 and the sequence number in column "No." is 678, then value I want to insert into column "ID" is 2201201408050678.
I need to use the column header as a reference in this code to ensure that the function works correctly when the column index changes.
Issue: When changing many rows, this code simply repeats the original function for each selected cell. It gets the job done, but not too quickly.
How can I improve code speed when a multi-row update occurs?
No.
Timestamp
ID
Status
20
24:01:22 15:01:30
2201241501300020
Approved
17
Process
16
24:01:22 15:59:10
2201241559100016
Approved
16
function neworder2_onEdit(e) {
var sheet = e.range.getSheet();
if ((sheet.getSheetName() == 'RETAIL_ORDER') || (sheet.getSheetName() == 'HAMPER_ORDER') || (sheet.getSheetName() == 'SEA FOOD_ORDER') || (sheet.getSheetName() == 'GARDEN_ORDER'))
{
var col = e.range.columnStart;
var col_header = sheet.getRange(1,col).getValue();
if (col_header != 'Status') return;
var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0];
var timestamp_col = headers.indexOf('Timestamp') + 1;
var num_col = headers.indexOf('No.') + 1;
var id_col = headers.indexOf('ID') + 1;
var row_start = e.range.rowStart;
var row_end = e.range.rowEnd;
if (sheet.getRange(row_start,col).getValue() != 'Approved') return;
var tz = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone();
var timestamp = Utilities.formatDate(new Date(), tz, 'yy-MM-dd HH:mm:ss');
for (let row = row_start; row <= row_end; row++) {
var timestamp_cell = sheet.getRange(row, timestamp_col);
if (timestamp_cell.getValue() !== '') continue;
timestamp_cell.setValue(timestamp).setNumberFormat('yy:MM:dd HH:mm:ss');
var num = sheet.getRange(row,num_col).getValue().toString().padStart(4,'0');
var id = timestamp.replace(/\D/g,'') + num;
var id_cell = sheet.getRange(row,id_col);
id_cell.setValue(id);
}
}
}
I believe your goal is as follows.
Your script works fine. You want to reduce the process cost of the script.
In this case, how about the following modification?
Modified script:
function neworder2_onEdit(e) {
var sheet = e.range.getSheet();
if (['RETAIL_ORDER', 'HAMPER_ORDER', 'SEA FOOD_ORDER', 'GARDEN_ORDER'].includes(sheet.getSheetName())) { // Modified
var col = e.range.columnStart;
var col_header = sheet.getRange(1, col).getValue();
if (col_header != 'Status') return;
var row_start = e.range.rowStart;
var row_end = e.range.rowEnd;
// I modified below script.
var values = sheet.getRange(row_start, 1, row_end - row_start + 1, 4).getValues();
if (!values.some(r => r[3] == 'Approved')) return;
var tz = e.source.getSpreadsheetTimeZone();
var timestamp = Utilities.formatDate(new Date(), tz, 'yy-MM-dd HH:mm:ss');
var res = values.map(([a, b, c]) => (a == "" || b != "") ? [b, c] : [timestamp, timestamp.replace(/\D/g, '') + a.toString().padStart(4, '0')]);
sheet.getRange(row_start, 2, row_end - row_start + 1, 2).setValues(res);
sheet.getRange(row_start, 2, row_end - row_start + 1, 1).setNumberFormat('yy:MM:dd HH:mm:ss');
}
}
In this modification, after the array was created using the script in your for loop, the array was put to the sheet.
Reference:
map()
Added 1:
From your following replying,
When "status", "ID" or "timstamp" column index change, in case I want to insert column then our code not working. Can we use column header (status, timestamp, ID, No.) as pramameter for my cript? Can you give suggestion to do this?
In this case, how about the following sample script?
Sample script:
function onEdit(e) {
var sheet = e.range.getSheet();
if (['RETAIL_ORDER', 'HAMPER_ORDER', 'SEA FOOD_ORDER', 'GARDEN_ORDER'].includes(sheet.getSheetName())) { // Modified
var col = e.range.columnStart;
var header = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0].map(h => h.toLowerCase()); // Added
var obj = header.reduce((o, e, i) => (o[e] = i, o), {});
var col_header = header[col - 1]; // Modified
if (col_header != 'status') return;
var row_start = e.range.rowStart;
var row_end = e.range.rowEnd;
// I modified below script.
var values = sheet.getRange(row_start, 1, row_end - row_start + 1, header.length).getValues();
if (!values.some(r => r[obj["status"]] == 'Approved')) return;
var tz = e.source.getSpreadsheetTimeZone();
var timestamp = Utilities.formatDate(new Date(), tz, 'yy-MM-dd HH:mm:ss');
var res = values.map(r => {
if (!(r[obj["no."]] == "" || r[obj["timestamp"]] != "")) {
r[obj["timestamp"]] = timestamp;
r[obj["id"]] = timestamp.replace(/\D/g, '') + r[obj["no."]].toString().padStart(4, '0');
}
return r;
});
sheet.getRange(row_start, 1, row_end - row_start + 1, res[0].length).setValues(res);
sheet.getRange(row_start, [obj["timestamp"]] + 1, row_end - row_start + 1).setNumberFormat('yy:MM:dd HH:mm:ss');
}
}
In this script, from your question, it supposes that the header values are No.,Timestamp,ID,Status. Please be careful this.
Added 2:
From your following new issue,
It's working but there is some issue with my sheet. when scipt ran that paste value to all column, some of theme using arrayformula so all column use arrayformula will get error "#REF!" Can we just paste value to column timestame, id.
In this case, how about the following sample script?
Sample script:
function onEdit(e) {
var sheet = e.range.getSheet();
if (['RETAIL_ORDER', 'HAMPER_ORDER', 'SEA FOOD_ORDER', 'GARDEN_ORDER'].includes(sheet.getSheetName())) { // Modified
var col = e.range.columnStart;
var header = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0].map(h => h.toLowerCase()); // Added
var obj = header.reduce((o, e, i) => (o[e] = i, o), {});
var col_header = header[col - 1]; // Modified
if (col_header != 'status') return;
var row_start = e.range.rowStart;
var row_end = e.range.rowEnd;
// I modified below script.
var values = sheet.getRange(row_start, 1, row_end - row_start + 1, header.length).getValues();
if (!values.some(r => r[obj["status"]] == 'Approved')) return;
var tz = e.source.getSpreadsheetTimeZone();
var timestamp = Utilities.formatDate(new Date(), tz, 'yy-MM-dd HH:mm:ss');
var res = values.map(r => {
if (r[obj["no."]] != "" && r[obj["timestamp"]] == "" && r[obj["status"]] == "Approved") {
r[obj["timestamp"]] = timestamp;
r[obj["id"]] = timestamp.replace(/\D/g, '') + r[obj["no."]].toString().padStart(4, '0');
}
r.shift();
return r;
});
sheet.getRange(row_start, 2, row_end - row_start + 1, res[0].length).setValues(res);
sheet.getRange(row_start, [obj["timestamp"]] + 2, row_end - row_start + 1).setNumberFormat('yy:MM:dd HH:mm:ss');
}
}
I am using this function which works upon submitting the entry via Google Forms. Currently the below script is adding increment number to the Sheet1 Last empty row
I want to include two more sheets where same incremental number will be added to last empty row.
Column will be same where increment number is pasting that is Column A but Sheet1 data starts from Row2 and Sheet2 and Sheet3 Data starts from row6.
Your help will be much appreciated.
function uPDATEiT() {
var aiColumnName = 'A'; //Sheet1,Sheet2,Sheet3 same column
var requieredColName = 'C' //it is just for Sheet1
var ss = SpreadsheetApp.getActiveSpreadsheet()
var worksheet = ss.getSheetByName('Sheet1','Sheet2','Sheet3')
var aiColRange = worksheet.getRange(aiColumnName + '1:' + aiColumnName + '1000');
var aiCol = aiColRange.getValues();
var aiColIndex = aiColRange.getColumn();
var reqCol = worksheet.getRange(requieredColName + '1:' + requieredColName + '1000').getValues();
var maxSeq = 0;
for (var i = 0; i <= aiCol.length; i++) {
if (parseInt(aiCol[i], 10) > maxSeq) { maxSeq = aiCol[i]; }
}
for (var i = 0; i <= aiCol.length; i++) {
if (('' + reqCol[i]).length > 0 && ('' + aiCol[i]).length === 0) {
maxSeq++;
worksheet.getRange(i + 1, aiColIndex).setValue(maxSeq);
}
}
}
You can use this formula in Sheet2!A4 and Sheet3!A4:
={"ID's";ARRAYFORMULA(IF(B5:B<>"",vlookup(B5:B,{'Data Sheet (Sheet1)'!$B$2:$B,'Data Sheet (Sheet1)'!$A$2:$A},2,false),""))}
What it does?
Check if column B is not empty, then get the id from Sheet1 based on the matched name(column B) as a key in the vlookup()
Output:
Original Sheet2:
Sheet 2 using the formula:
Update
If you just want to append your maxSeq in Sheet2, you can use this:
Code:
function uPDATEiT() {
var aiColumnName = 'A'; //Sheet1,Sheet2,Sheet3 same column
var requieredColName = 'C' //it is just for Sheet1
var ss = SpreadsheetApp.getActiveSpreadsheet()
var worksheet = ss.getSheetByName('Data Sheet (Sheet1)')
var sheet2 = ss.getSheetByName('Sheet2')
Logger.log(worksheet)
Logger.log(sheet2.getLastRow())
var aiColRange = worksheet.getRange(aiColumnName + '1:' + aiColumnName + '1000');
var aiCol = aiColRange.getValues();
var aiColIndex = aiColRange.getColumn();
var reqCol = worksheet.getRange(requieredColName + '1:' + requieredColName + '1000').getValues();
var maxSeq = 0;
for (var i = 0; i <= aiCol.length; i++) {
if (parseInt(aiCol[i], 10) > maxSeq) { maxSeq = aiCol[i]; }
}
for (var i = 0; i <= aiCol.length; i++) {
if (('' + reqCol[i]).length > 0 && ('' + aiCol[i]).length === 0) {
maxSeq++;
worksheet.getRange(i + 1, aiColIndex).setValue(maxSeq);
sheet2.getRange(sheet2.getLastRow()+1,aiColIndex).setValue(maxSeq);
}
}
}
Get the last row in the sheet that contains data using getLastRow() , then increment it by 1.
Output:
I'm using the YouTube API with Google sheets to pull a list of all my videos by using YouTube.PlaylistItems.list with the playlistId parameter set to that of my uploads page, and I have a loop to go through page by page and print selected aspects of each item in a sheet.
The problem is that along the way (starting at around the 250th video), it returns ids that had already been returned and seems to think they are present at multiple different positions in the playlist, which they are not (this is not possible with the uploads list).
This was working no problem before, so I'm guessing it's a bug, but I've put my code down below in case anyone can spot anything wrong with it. Any help would be much appreciated
function retrieveInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Imported')
sheet.getRange('A2:H').clearContent();
SpreadsheetApp.flush();
var nextPageToken = '';
var page = 0
while (nextPageToken != null) {
var playlistResponse = YouTube.PlaylistItems.list('snippet,status', {
playlistId: 'UU2yLF7zgs3Uz1c_eK75g2Sw',
maxResults: 50,
pageToken: nextPageToken
});
for (var j = 0; j < playlistResponse.items.length; j++) {
var playlistItem = playlistResponse.items[j];
sheet.getRange((page * 50) + (j + 2), 1).setValue(playlistItem.snippet.resourceId.videoId);
sheet.getRange((page * 50) + (j + 2), 2).setValue(playlistItem.snippet.title);
sheet.getRange((page * 50) + (j + 2), 3).setValue(playlistItem.snippet.description);
sheet.getRange((page * 50) + (j + 2), 4).setValue(playlistItem.status.privacyStatus);
sheet.getRange((page * 50) + (j + 2), 12).setValue(playlistItem.snippet.position);
}
nextPageToken = playlistResponse.nextPageToken;
var page = page + 1
}
SpreadsheetApp.flush();
// Stage 2
var nextPageToken2 = ''
var page2 = 0
while (nextPageToken2 != null) {
var bpoint = sheet.getRange((page2 * 50) + 2, 1).getValue()
if (bpoint == "") {
break
}
var ids = sheet.getRange(2 + (50 * page2), 1, 50, 1).getValues().toString();
var videos = YouTube.Videos.list('snippet,contentDetails,fileDetails', {
id: ids,
maxResults: 50,
pageToken: nextPageToken2
});
for (var k = 0; k < videos.items.length; k++) {
var videoitem = videos.items[k]
var durationinfo = videoitem.contentDetails.duration
var langinfo = videoitem.snippet.defaultAudioLanguage
var filename = videoitem.fileDetails.fileName
sheet.getRange((page2 * 50) + (k + 2), 5).setValue(durationinfo)
sheet.getRange((page2 * 50) + (k + 2), 7).setValue(langinfo)
sheet.getRange((page2 * 50) + (k + 2), 8).setValue(filename)
}
nextPageToken = videos.nextPageToken;
var page2 = page2 + 1
}
SpreadsheetApp.flush();
}
So I'm currently trying to set up a simple button which moves data from a range into another sheet for future reference. I've got it working if the sheet it is going to contains no information but I wanted to put all of the references into one sheet together.
Is there a way to add to the bottom of a range of data? This is what I have at the moment.
function Addvoucher() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Meta');
var source_sheet = ss.getSheetByName("Tools");
var source_range = source_sheet.getRange("B9:D9");
var target_range = target_sheet.getRange("S3:U3");
var last_row = target_sheet.getLastRow();
var target_range = target_sheet.getRange("S3" + (last_row + 1) + ":U3" + (last_row + 1));
target_sheet.insertRowAfter(last_row);
source_range.copyTo(target_range);
}
Modification points:
I think that in your script, how about modifying "S3" + (last_row + 1) + ":U3" + (last_row + 1) of target_sheet.getRange("S3" + (last_row + 1) + ":U3" + (last_row + 1)) as follows?
In the current script, for example, last_row + 1 is 2, "S3" + (last_row + 1) + ":U3" + (last_row + 1) becomes S32:U32. In this case, it is required to be S2:U2. I think that this might be the reason of your issue.
Tried this, the issue is there is data in other columns to the left of this "block" of information thus this gets put onto row 100 as its the first available row.
From your replying, I could understand that in your Spreadsheet, the columns except for the columns "U" to "S" has the values and the last row of the columns "U" to "S" is different from other columns.
When above points as reflected to your script, I would like to propose to modify as follows.
From:
var last_row = target_sheet.getLastRow();
var target_range = target_sheet.getRange("S3" + (last_row + 1) + ":U3" + (last_row + 1));
To:
var last_row = target_sheet.getLastRow();
var values = target_sheet.getRange("S1:U" + last_row).getValues();
for (var i = values.length - 1; i >= 0; i--) {
if (values[i].every(e => e.toString() != "")) {
last_row = i + 1;
break;
}
}
var target_range = target_sheet.getRange("S" + (last_row + 1) + ":U" + (last_row + 1));
I want to send email reminder if the date columns is 7 or 1 days away from today. I have already made the script and what i want to add is it should consider others columns too, not only 1 with one script, for sending reminder for respective columns.
For eg:
It should remind for Plan Date, Plan Date 1, Plan Date 2 and Plan Date 3.
Please see the Sample Attached.
Script:
function checkReminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// fetch this sheet
var sheet = spreadsheet.getSheets()[0];
// figure out what the last row is
var lastRow = sheet.getLastRow();
// figure out what the last column is
var lastCol = sheet.getLastColumn();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 2
var startRow = 2;
// the columns are indexed starting at 2, and the first column
// is the headers, so start with column 2
var startCol = 2;
// grab column 3 (the 'days left' column)
var range = sheet.getRange(2,3,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();
// Now, grab the reminder name column
range = sheet.getRange(2, 1, lastRow-startRow+1, 1);
var reminder_info_values = range.getValues();
// Now, grab the first row
range = sheet.getRange(1, 2, lastCol-startCol+1, 1);
var column_info_values = range.getValues();
var warning_count = 0;
var msg = "";
// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left == 1) {
// if it's exactly 1, do something with the data.
var reminder_name = reminder_info_values[i][0];
var column_name = column_info_values[0][0];
msg = msg + "Reminder: "+reminder_name+" - "+column_name+" is due in "+days_left+" day.\n";
warning_count++;
}
}
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left == 7) {
// if it's exactly 7, do something with the data.
var reminder_name = reminder_info_values[i][0];
var column_name = column_info_values[0][0];
msg = msg + "Reminder: "+reminder_name+" - "+column_name+" is due in "+days_left+" days.\n";
warning_count++;
}
}
if(warning_count) {
MailApp.sendEmail("myidsample#gmail.com",
"Reminder Spreadsheet Message", msg);
}
};
You're currently fetching only single column and processing that only.
In order to make this script run over other columns too, you'll need to loop through them, fetch data and then process them one by one.
Here you're fetching data from 3rd column :
// grab column 3 (the 'days left' column)
var range = sheet.getRange(2,3,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();
Like wise you can run a loop till last column and fetch data from 3rd, 5th, 7th .. columns and keep processing them.
So the code goes like this
function checkReminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// fetch this sheet
var sheet = spreadsheet.getSheets()[0];
// figure out what the last row is
var lastRow = sheet.getLastRow();
// figure out what the last column is
var lastCol = sheet.getLastColumn();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 2
var startRow = 2;
// the columns are indexed starting at 2, and the first column
// is the headers, so start with column 2
var startCol = 2;
// Now, grab the reminder name column
range = sheet.getRange(2, 1, lastRow - startRow + 1, 1);
var reminder_info_values = range.getValues();
// Now, grab the first row
range = sheet.getRange(1, 2, lastCol - startCol + 1, 1);
var column_info_values = range.getValues();
var warning_count = 0;
var msg = "";
for (var u = 3; u <= lastCol; u += 2) {
// grab the day's left column
var range = sheet.getRange(2, u, lastRow - startRow + 1, 1);
var numRows = range.getNumRows();
var days_left_values = range.getValues();
// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if (days_left == 1) {
// if it's exactly 1, do something with the data.
var reminder_name = reminder_info_values[i][0];
var column_name = column_info_values[0][0];
msg = msg + "Reminder: " + reminder_name + " - " + column_name + " is due in " + days_left + " day.\n";
warning_count++;
}
}
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if (days_left == 7) {
// if it's exactly 7, do something with the data.
var reminder_name = reminder_info_values[i][0];
var column_name = column_info_values[0][0];
msg = msg + "Reminder: " + reminder_name + " - " + column_name + " is due in " + days_left + " days.\n";
warning_count++;
}
}
if (warning_count) {
MailApp.sendEmail("myidsample#gmail.com",
"Reminder Spreadsheet Message", msg);
}
}
}
This is un-tested code, let me know if any issue arises. I'll be happy to help you.
Thank you!