Importrange from multiple URLs using Apps Script - javascript

I am a newbie for coding and GAS. I can solve the issue via google spreadsheet quite well, but I am looking for better and efficient ways to solve the problem via GAS.
This is the script I wrote so far.
function myFunction() {
const app= SpreadsheetApp;
const activeSheet = app.getActiveSpreadsheet().getActiveSheet();
const temp = activeSheet.getRange("AE4:AE").getValues();
}
As you can see it from a picture, there are URLs and each URL contains a value which need to be pulled out. I want to importrange values from cell "C6" from each URL and print it to the left column. Key to this problem is that one value needs to be imported from each URL to the left.
Google SpreadSheet
= IMPORTRANGE("URL", "Sheet1!C6")

Puts data in the active cell from Sheet1!C6
function myfunk() {
const ss = SpreadsheetApp.getActive();
const cell = ss.getActiveCell();
cell.setValue(ss.getRange("Sheet1!C6").getValue())
}
Note: this is not a custom function...not meant to be run by placing in cell of spreadsheet.

Related

Is it possible to merge multiple google docs into one using Apps Script?

I wanted to the question as formulated in the title, let me elaborate further.
I selected the response destination from Forms into a new spreadsheet. Using Google Script I created an add-on (named AutoFill Docs) for this spreadsheet and it "fills out" the Docs template I made and saves itself as a new, separate Docs file in a designated folder.
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
const Responses = DriveApp.getFileById('XXXXX');
const destinationFolder = DriveApp.getFolderById('XXX')
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
const rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index){
//skipping header
if (index === 0) return;
//Using the row data in a template literal, making a copy of template document in destinationFolder
const copy = Responses.makeCopy(`${row[1]}, ${row[0]} XXXX (XXXX)` , destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
//replacing my replacement tokens with values from the spreadsheet row
body.replaceText('{{Timestamp}}', row[0]);
body.replaceText('{{Whats your name?}}', row[1]);
//end so on
doc.saveAndClose();
const url = doc.getUrl();
//making documents links
sheet.getRange(index + 1, 16).setValue(url)
})
}
My issue is, that I want all those filled-out templates for every response to save in one docs file (e.g. one response=one new extra page), rather than a new doc file.
Of course, I can include links to those docs in the final one or copy and paste, but I prefer it to be automatic.
So here are my concerns: Is it even possible? If yes, how should I modify my existing code to do that or what should I look for?
P.S. I know the "createNewGoogleDocs" is basically the opposite of what I want, but this is the only function I managed to find, but it is just a partial solution to my case. I am a complete newbie, so any guidance/suggestions are very much appreciated, I'm completely stuck.

Renaming Google Sheets File Automatically From A Cell

I'm a complete novice to script, so please forgive my ignorance.
I'm trying to run a script so that when a new sheet is made of a specific set of sheets I use for my clients, that new copy renames itself upon the input of a specific cell (C13) where my clients will enter their name.
I should add I was using the following code that i grabbed from a similar question on here:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var C13 = ss.getRange("C13").getValue();
var name = C13;
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet("name");
}
But this only worked for when the sheet opened, and obviously with nothing in C13 this isn't useful
Thank you
If you wish that the sheet gets renamed when a specific range gets edited than you can use onEdit() trigger.
The code below will replace the sheet's name with C13 cell's value if you change it.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var name = ss.getRange("C13").getValue();
if(e.range.getA1Notation() == 'C13' && name!= '') {
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(name);
}
}

Google sheet copy data from table to other tab while respecting the format

I was hoping if I could get a bit of help on a script I wrote in google app scripts. I have a google sheet with two tabs "TESTPLANNING" and "Sheet3". My goal is to copy the table I have in the "TESTPLANNING" to "Sheet3" at the click of a button all while respecting the formatting of the table in my first sheet.
I have attached an image of my two sheets, the top image represents the table in "TESTPLANNING" tab and bottom image is in "Sheet3". https://imgur.com/a/vO1RvWg
So far I have a working script which clears my table of all values, I activate this by clicking the "reset" button. I want the "submit" button submit my table to "Sheet3" in the same table format.
So far my code copies the values from range A3:H7 (shown in lower screenshot), however I want an exact copy of the table in "sheet3" (not just the values). How can I achieve this ? I'm relatively new to this so any help would be appreciated, thanks !
Working code for submit button:
function submit() {
const ss = SpreadsheetApp.getActive();
const cal_sh = ss.getSheetByName('TESTPLANNING');
const raw_sh = ss.getSheetByName('Sheet3');
const ranges = ['A3:H7'];
const values = [];
ranges.forEach(r=>values.push(...cal_sh.getRange(r).getValues().flat()));
raw_sh.getRange(raw_sh.getLastRow()+1,1,1,values.length).setValues([values]);
}
function copyFromOneSheetToAnother() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('TESTPLANNING');
const rg = sh.getRange('A3:H7');
const osh = ss.getSheetByName('Sheet3');
const org = osh.getRange('A3');//only need upper left corner
rg.copyTo(org);
}
Range.coptyTo()

Sheet name is not updating with the script

In my google spreadsheet i created a script (below) which give me the function/formula to get the name of the sheet/tab which i am currently in.
The problem is when i change the sheet name to a new name then this cell is not updating with the new name even after several refresh.
I have set triggers to run the script as well as i have used SpreadsheetApp.flush() to forcefully applies all pending Spreadsheet changes, but still this does not work.
This is my script below:
function sheetname() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveRange().getSheet();
return s.getName();
}
And this is with the flush :
function sheetname() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveRange().getSheet();
SpreadsheetApp.flush();
return s.getName();
}
Please let me know what changes i need to make so that this can update as and when the sheet name is changed.
This function works when connected to an onChange trigger and the Sheet Name is changed. I think you were adding it to the cell as "=sheetName()". I have to admit I don't know much about custom functions because I always do all of my calculations in arrays because it's much faster. But if you hook this up to an onChange event and set the load value of the sheet name into a cell it works.
function sheetName()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sht=ss.getActiveSheet();
var name=sht.getName();
sht.getRange('A5').setValue(name);
}

Accessing spreadsheet in Google Script

I am writing a google script on top of a spreadsheet.
I want to deploy it as a web app.
It shows some values.
Unfortunately, with my current code, google reminds me:
TypeError: Cannot call method "getSheetByName" of null.
I have no idea where the mistake is.
Here's the code
function doGet(e) {
var app = UiApp.createApplication().setTitle('Details');
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var dataFromCell = ss.getRange("B4").getValue();
var mypanel = app.createVerticalPanel();
var label = app.createLabel(dataFromCell);
app.add(mypanel);
app.add(label);
return app;
}
In standalone webapps you cannot use getActiveSpreadsheet because no one is using it actively...
Use SpreadsheetApp.openById('ID') instead, you can get the ID in the url of your spreadsheet like in this example :
https://docs.google.com/spreadsheet/ccc?key=0AnqSFd3iikE3d-------nZIV0JQQ0c1a3dWX1dQbGc#gid=0
between key= and #, ie 0AnqSFd3iikE3d-------nZIV0JQQ0c1a3dWX1dQbGc
Not need to use ID , just try this code ( change mygooglelocation with your Spreadsheet name and range of cells. Working very well for me with google maps...
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('mygooglelocation');
var ss = SpreadsheetApp.getActive();
var mylocationInfo = ss.getRange("A2:B4").getValues();

Categories

Resources