This question already has an answer here:
Script to run on specific sheet
(1 answer)
Closed 6 months ago.
I'm trying to put together a code and want the code to choose a selected sheet rather than the active sheet - the Sheet that I want the code to run from has been renamed 'Training Record':
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
The rest of the code is as follows and runs as intended without the change that I need:
function createTrainingDocs() {
var tempID ='1dyQIK3OCPWXDLydg-MKRhL43CACtyQQDG3VUR11PlH8'
var folderID = '1UpVdcyuP-c_PZVx8uEBRVEH7m_zBnzvb'
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
//console.log(data)
data.forEach((row,i)=>{
console.log(row[2]+', '+row[1]+' Training Log')
var newDocID = DriveApp.getFileById(tempID).makeCopy(row[2]+', '+row[1]+' Training Log',DriveApp.getFolderById(folderID)).getId()
var body = DocumentApp.openById(newDocID).getBody()
body.replaceText('{{Name}}',row[1]+' '+row[2])
body.replaceText('{{School Name}}',row[0])
SpreadsheetApp.getActiveSheet().getRange(i+1,4).setValue(newDocID)
})
}
Change
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
To
var spread = SpreadsheetApp.getActiveSpreadsheet();
var data = spread.getSheetByName("Training Record").getDataRange().getValues()
Related
This question already has answers here:
Running multiple sheets under one google script, syntax error
(2 answers)
TypeError: Cannot find function includes in object
(5 answers)
Closed 6 months ago.
Could you advise please why I get syntax error for => in google script in the below code? If I remove the filter part (values = values.filter(e=>e[0]);) the functions work well. In another file the code with the filter works fine.
function main(){
moveData();
clearRange1();
}
function moveData () {
var ss = SpreadsheetApp.getActive();
var dailySheet = ss.getSheetByName('Վաճառքի գներ');
var appendSheet = SpreadsheetApp.openById('1fpJpb5buD236iO7sHuANdqkPEd2QEQ-QewIIl7us_UQ').getSheetByName('Sheet1');
var values = dailySheet.getRange("Վաճառքի գներ!3:" + dailySheet.getLastRow()).getValues();
values = values.filter(e=>e[0]); //gets rid of blank rows. filters based on the first column (index 0).
appendSheet.getRange(appendSheet.getLastRow()+1,1,values.length,values[0].length).setValues(values);
}
function clearRange1() { //replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.openById('1bdsEQOq7YeVtu0bxH67zrLqlW7uhHBdu0Uwu6ck87UU').getSheetByName('Sheet1')
sheet.getRange('A3:S').clearContent();
var sheet = SpreadsheetApp.openById('1p53h8P_64WC7hG7IwIYxRcb08uGN25oXrH6mtL7u2lQ').getSheetByName('Sheet1')
sheet.getRange('C4:E').clearContent();
}
So I have a google script code that grabs data from one google sheet into a matrix and copy and paste selected segments from that matrix into cells in another google sheet. The issue I have is that in the sheet that the script grabs data from there are some dates in the data with no associated time. These end up in the variables Trailer1 through Trailer5. When they get pasted in into the new sheet they change into a date and time variable and date is always one day behind and the time is 23:00 hours. To me it seems like the script is translating the dates and times into whatever timezone the script is in and is assuming the dates entered in the original sheet are at 00:00 on the date entered. I don't care what the time stamp is or if there even is one, I just need the dates to match. How do I fix this?
function myCopyPaste() {
var now= new Date();
var year = now.getFullYear();
var startrow = 3;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(year) //get current years sheet
var id = sheet.getRange('D1').getValue();
var length = sheet.getRange('B1').getValue()
var boxsize = length-startrow;
var rawsheet = SpreadsheetApp.openById(id).getSheetByName(year);
var rng = rawsheet.getRange(length, 1, 1,87 )
var rangeArray = rng.getValues();
var ANUSED = rangeArray[0][19]
var RunTime = rangeArray[0][27]
var Railcar1 = [rangeArray[0].slice(5,7)];
var Railcar2 = [rangeArray[0].slice(13,15)];
var Production = rangeArray[0][26];
var MedInv = rangeArray[0][30];
var HPInv = rangeArray[0][33];
var Trailer1 = [rangeArray[0].slice(36,43)];
var Trailer2 = [rangeArray[0].slice(44,51)];
var Trailer3 = [rangeArray[0].slice(52,59)];
var Trailer4 = [rangeArray[0].slice(60,67)];
var Trailer5 = [rangeArray[0].slice(68,75)];
var TubeTrailer = [rangeArray[0].slice(76,81)];
var Comments = rangeArray[0][86];
if (RunTime>0) {
sheet.getRange(length,4).setValue(ANUSED)
}
else {sheet.getRange(length,4).setValue(0)
}
sheet.getRange(length,5,1,2).setValues(Railcar1);
sheet.getRange(length,7,1,2).setValues(Railcar2);
if (RunTime>0) {
sheet.getRange(length,9).setValue(Production)
}
else {sheet.getRange(length,9).setValue(0)
}
sheet.getRange(length,10).setValue(RunTime);
sheet.getRange(length,14).setValue(MedInv);
sheet.getRange(length,15).setValue(HPInv);
sheet.getRange(length,19,1,7).setValues(Trailer1);
sheet.getRange(length,26,1,7).setValues(Trailer2);
sheet.getRange(length,33,1,7).setValues(Trailer3);
sheet.getRange(length,40,1,7).setValues(Trailer4);
sheet.getRange(length,47,1,7).setValues(Trailer5);
sheet.getRange(length,54,1,5).setValues(TubeTrailer);
sheet.getRange(length,65).setValue(Comments);
}
I'm pretty sure I figured this out. I went to file spreadsheet settings and found that the spreadsheet being imported was in mountain time and one being pasted into was in pacific. I changed the pacific spreadsheet time zone to mountain time and it appears to have fixed the issue.
For some context, I have a chart with originally 1 data range, which my existing macro adds a second data range to.
I now want to delete the charts original data range, so that it is only displaying the new data range. Is this possible to do via Google App Script?
Just to explicitly answer this question I copied #Tanaike portion of the answer that solves your issue.
Pattern
var daily_data = spreadsheet.getSheetByName("Daily Data");
var LTD_data = spreadsheet.getSheetByName("Long Term Data");
var chart = daily_data.getCharts()[0];
var range = LTD_data.getRange("B2:J3")
var ranges = chart.getRanges();
chart = chart.modify();
ranges.forEach(function(range) {chart.removeRange(range)});
var modifiedChart = chart.addRange(range).build();
daily_data.updateChart(modifiedChart);
References:
updateChart(chart)
removeRange(range)
I am stuck on getting anything to copy from a closed Google Sheet to an open one.
This is my attempted script which the debugger does not get past line 2:
function lloydsRestore() {
var ss =SpreadsheetApp.openById("1ZRHHA1fcXXCXwD4nCjjtIg67DHvtm5pLjmwzJiD3ZgA").getActiveSheet();
var ls = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Lloyds');
var values= ls.setActiveSelection("A1:T72").getvalues;
var bs = App.getActiveSpreadsheet().getSheetByName('BS');
var bsput = bs.getRange("A1:T72").setValues(values);
}
You are nearly there - just a few misunderstandings in your script.
You are opening the sheet (ss) so you use ss.getSheetByName(...) rather than SpreadsheetApp.getActiveSheet().getSheetByName(...)
You aren't calling getValues correctly - you need the ()
function lloydsRestore() {
var ss =SpreadsheetApp.openById('1ZRHHA1fcXXCXwD4nCjjtIg67DHvtm5pLjmwzJiD3ZgA');
var ls = ss.getSheetByName('Lloyds');
var values = ls.getRange('A1:T72').getValues();
var bs = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('BS');
var bsput = bs.getRange('A1:T72').setValues(values);
}
You may not need a script for this however as the IMPORTRANGE function does exactly what your function does. Put the following in a cell and you will get your values imported.
=IMPORTRANGE('https://docs.google.com/spreadsheets/d/1ZRHHA1fcXXCXwD4nCjjtIg67DHvtm5pLjmwzJiD3ZgA', 'Lloyds!A1:T72')
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm creating a projects database using google sheets and Apps Script.
I have developed the code below to add a new record page (sheet) every time a new record/project is added to the projects list sheet.
The strange thing is that the code works well but when I declare the variable text in line 11 the code doesn't work. Actually, the variable text is not even used in the code yet. it doesn't contribute to the code's logic.
Can anyone explain this to me, please?
// This module is to add a new project record page when a new project included in the Project List sheet
function addNewRecord(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getActiveSheet().getSheetName();
var sheet = e.source.getSheetByName("Template");
var row = e.range.getRow();
var col = e.range.getColumn();
var cell = e.source.getActiveSheet().getRange(row,1).getValue();
var text = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Projects List').getRange(row,1).getCell(row,1);
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets(); //creating an array or calling the existing sheets of the spreadsheet.
var sheetsNames = []; //creating an array of the existing sheets names
for (let i =0 ; i < sheets.length; i++){
sheetsNames[i] = sheets[i].getSheetName();
}
//checking the entered value contains LCE keyword and it's an actual new record
if (sheetName === 'Projects List' && col ===1){
if (cell.includes('LCE') && sheetsNames.indexOf(cell) === -1){
sheet.copyTo(ss).setName(cell);
ss.getSheetByName(cell).getRange(1,2).setValue(cell);
function getSheetUrl() {
var SS = SpreadsheetApp.getActiveSpreadsheet();
var ss = SS.getSheetByName(cell);
var url = '';
url += SS.getUrl();
url += '#gid=';
url += ss.getSheetId();
//Logger.log(url);
return url;
}
var value = SpreadsheetApp.newRichTextValue().setText(cell).setLinkUrl(getSheetUrl()).build();
//e.source.getSheetByName('Projects List').getRange(row,1).getCell(row,1).setRichTextValue(value);
//cellV.setRichTextValue(value);
}
}
//Logger.log(sheets);
}
looks like your variable "image" is a UIImageView, not a UIImage. Replace this line
var myRGBA = RGBAImage(image: image)
with this
var myRGBA = RGBAImage(image: image.image)
This is because a UIImageView holds an image. To access it you must call UIImageView.image
For future reference, you should always copy and paste code, not posting a screenshot, so that other users can find questions easier through search engines, and users trying to answer your questions can replicate your issue by copying and pasting your code into their own environment.