Get Column Range Based on Active Cell - javascript

I am trying to copy some data to the clipboard, from a Google Spreadsheet, by running a script - I want to minimize user error, this is the reason for the script. Depending on the active cell, I want the script to select 57 rows below the active cell and copy the values to the clipboard, in order to be pasted somewhere else. I cannot figure out how to do this. Here is the code that I have at the moment.
function copyDailySubsidy() {
var ss = SpreadsheetApp.getActive();
var rangeRow = ss.getActiveCell().getRow();
var rangeCol = ss.getActiveCell().getColumn();
var range = ss.getRange(rangeRow, rangeCol, 23).getValues();
//var values = ss.getRange().getValues();
Logger.log(rangeCol);
}
I can't seem to figure out how to copy the values of the range, or even select the appropriate range. Any help that you can afford would be greatly appreciated!

This is not achievable in Google Apps Script. Your script is running on a remote server, and has no access to a user's clipboard.

Related

Is there a way to setEditors for a certain range using Google Scripts?

I'm trying to set Editors for a certain range on the sheet. I found this function called getEditors() that gives a list of all the editors for a certain defined range. Can we set editors too?
The class Protection features different methods that allow you to modify the editors of a spreadsheet range.
To set an editor you can use the method addEditor(), to delete
editors, use removeEditor.
Sample:
// Protect range A1:B10, then remove all other users from the list of editors.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var protection = range.protect().setDescription('Sample protected range');
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}

Google Sheets script getValue doesn't get value in UI (latest calculation)

I am writing a simple script that basically reads a value and send it to another sheet upon clicking a button. For testing purpose I wrote:
SpreadsheetApp.flush()
var sheet = localSheet.getSheetByName('Input');
var dateTimeRange = sheet.getRange(2,2);
var dateTimeValue = dateTimeRange.getDisplayValue();
localSheet.toast(readingValue, dataTypeValue)
In my spreadsheet setting, I set:
File->Spreadsheet settings->Calculation->On change and every minute
Cell in range 2,2 has
=now()
So, every minute, the value in cell 2,2 recalculates itself.
But when I run the script, the output value that I see is always the value of the last time I modified the sheet. As long as I don't interact with the sheet, the script is unable to read the new value. I tried adding flush(), doesn't work. Anyone have any idea?
Thank you.
I think that the reason of your issue is that the sheet is not updated. In your case, unfortunately, flush() cannot be used. In order to avoid this issue, how about the following workarounds? Please think of this as just one of several answers.
In your situation, when the sheet is edited, the value retrieving by getDisplayValue() is updated. In this answer, this is used.
Workaround 1:
In this workaround, the cell "B2" is overwritten by =NOW().
Modified script:
var localSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = localSheet.getSheetByName('Input');
var dateTimeRange = sheet.getRange(2, 2);
var dateTimeValue = dateTimeRange.setFormula("=NOW()").getDisplayValue(); // Modified
localSheet.toast(dateTimeValue);
Workaround 2:
In this workaround, an empty cell is cleared. At sample script, the cell "B3" supposes the empty and this cell is not related to the main data. This cell is cleared. By this, let Spreadsheet know the sheet is edited.
Modified script:
var localSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = localSheet.getSheetByName('Input');
var dateTimeRange = sheet.getRange(2, 2);
sheet.getRange("B3").clear(); // Added
var dateTimeValue = dateTimeRange.getDisplayValue();
localSheet.toast(dateTimeValue);
Value of "B2" on the sheet "Input" is updated even if the edited cell is different from the main sheet. So you can also use localSheet.getSheetByName("Sheet1").getRange("A1").clear() instead of sheet.getRange("B3").clear().
Note:
It seems that your script might not be the latest one.
Is localSheet var localSheet = SpreadsheetApp.getActiveSpreadsheet()?
readingValue and dataTypeValue are not declared. In this answer, dateTimeValue is used.
In above workarounds, the value is updated. But the retrieved value shows the value of 4 or 5 seconds faster every time. About this, I couldn't find to do the same value with "B2". I apologize for this situation.
If this was not useful for your situation, I apologize.

Get random range google spreadsheet

I have managed with VBA and Excel to achieve my purposes, but I'd like to move to google spreadsheet for particular reasons.
I'm trying to replicate a code that works just fine in VBA.
It's simple, I have a sheet with a bank of questions in column A, and I'd like a macro that can select 1 random question and copy it to a second sheet.
I'm having trouble understanding how I can access a random cell, copy it and paste it to the second sheet. Some plain explanation would be appreciated since I have very little or non-existent knowledge of programming or javascript.
function test() {
var ss = SpreadsheetApp.openById("sheetID");
//Say I have 10 questions in the BANKSHEET, for instance
var rQuestion = Math.floor(Math.random()*10+1);
//What goes in A1? So that I can access the range randomly according to rQuestion value.
var inputRange = ss.getSheetByName("BANKSHEET").getRange("A1");
var inputValues = inputRange.getValues();
var outputRange = ss.getSheetByName("QUIZZ").getRange("A1").setValues(inputValues);
How about this modification? I think that there are several solutions for your situation. So please think of this as one of them.
Modification points :
Retrieve the number of cells at "A1:A" from sheet of "BANKSHEET".
Retrieve randomly one of the number.
Copy the cell value of the retrieved number to "A1" of "QUIZZ".
Modified script :
function test() {
var ss = SpreadsheetApp.openById("sheetID");
// Retrieve randomly a value from sheet of BANKSHEET
var sheet = ss.getSheetByName("BANKSHEET");
var src = sheet.getRange("A" + (1 + Math.floor(Math.random() * sheet.getLastRow())));
// Put the value to "A1" at sheet of QUIZZ
var dst = ss.getSheetByName("QUIZZ").getRange("A1");
src.copyTo(dst);
}
Note :
It supposes that there are values in only column A of "BANKSHEET".
This modified script randomly retrieves one value from column A of "BANKSHEET" every time.
If you don't want to use the value which has already been used, please modify the script for your situation.
Reference :
copyTo()
If I misunderstand your question, I'm sorry.

Dynamic Google Form using Google Apps Script

I have set up an item on my Google Form to set list based on values from a Google spreadsheet, code is:
function updateForm(){
var form = FormApp.openById("FormID");
var namesList = form.getItemById("ItemID").asListItem();
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("SourceSheetName");
var namesValues = names.getRange(2, 1, names.getMaxRows() - 1).getValues();
var List = [];
for(var i = 0; i < namesValues.length; i++)
if(namesValues[i][0] != "")
List[i] = namesValues[i][0];
namesList.setChoiceValues(List);
}
This works perfectly fine.
What I really want is to set each of the items listed on a spreadsheet to take you to a specific section on the Google Form.
I know this can be done manually on the Google Form by using 'Go To Section Based On Answer', but I was wondering if I can add a second column on the source spreadsheet to list the sections and amend my code accordingly?
EDIT - this is not a duplicate to another question which is mentioned below. The question asked previously had 2x choices set in the code. My question involves a set of responses set in a Googlesheet that auto populates the Google Form. What I really want is to auto set Go To Section Based on Answer automatically too. I hope this makes sense. Thanks

getActiveCell always returns A1

I'm using the new Google Sheets API and any every time I try to get the active cell, it just returns cell A1 in the first sheet, no matter what. Here's my code:
var s = SpreadsheetApp.getActiveSpreadsheet();
var ss = s.getActiveSheet();
var result = ss.getActiveCell().getValue();
Seems like a Google bug...
This is currently working perfectly for me. If I highlight a cell in the active sheet and run your code, the value in the cell is logged for me.
You must use getActiveSheet() to specify the sheet. You cannot specify the sheet with getSheetByName()

Categories

Resources