Search and change all links on the sheet. Google Sheets - google-apps-script

There is a table. There is a sheet in the table. Contains 203,354 cells (12 columns and 11962 rows). This sheet has links to cells within the table. Links look like this: #gid=1282039879&range=A41, #gid=1282039879&range=A67:E67, #gid=1282039879&range=A5375:E5375, #gid=1282039879&range=A11780:E11780 etc. Many links, 585 pieces. After copying the sheet, the #gid changes and the links don't work. We need a script for Apps Script that searches for all links on the sheet, determines the current #gid of the sheet and changes it in the links. Manually, this is easily done through "Find and Replace". But it needs to be automated. This script will be assigned to the button.
Table example - https://docs.google.com/spreadsheets/d/1iuPtdWyIo0SRbRrUzTp6hnP-iLn7B764G9j7w7jCkZE/edit?usp=sharing

You can write a script in Python to automate this process. To do this, you can use the openpyxl library to read the Excel file, find all the links in the sheet, extract the current gid, and update the links accordingly.
Here's a sample code that you can use as a starting point:
import openpyxl
# Load the workbook
wb = openpyxl.load_workbook("file.xlsx")
# Select the sheet
sheet = wb["Sheet1"]
# Get the current gid of the sheet
gid = sheet.parent._gid
# Loop through each cell in the sheet
for row in sheet.iter_rows(values_only=True):
for cell in row:
if isinstance(cell, str) and "#gid=" in cell:
# Replace the old gid with the new one
cell = cell.replace(f"#gid=1282039879", f"#gid={gid}")
# Save the workbook
wb.save("file_new.xlsx")
This script will open the Excel file, find all the cells with links, replace the old gid with the current gid, and save the updated file with a different name. You can further customize this script to match your requirements.

Related

Copying and pasting data in Google Sheets to another column

I have a dashboard that is reporting live data based on a growing customer database, and I am currently tracking the daily process by copying and pasting the data in the column into the next on a daily basis.
I'd like to create a script that automatically copies the values from an array (B:B) for example, into another sheet OR into the next available column to the right.
You may refer to the image below for further clarity.
Sample
I saw may threads on this but they were all about copying into a different row, as opposed to a different column.
Many thanks in advance!
The getLastRow() method is used to return the position of the last row that has content.
The getLastColumn() method is used to return the position of the last column that has content.
Therefore, in order to be able to copy the data into the next available column, I suggest you try this:
function copyColumn() {
var ss = SpreadsheetApp.openById("ID_OF_THE_SPREADSHEET");
var sheetFrom = ss.getSheetByName("SHEET_FROM");
var sheetTo = ss.getSheetByName("SHEET_TO");
var values = sheetFrom.getRange(NO_OF_THE_ROW_WHERE_DATA_STARTS, 3, sheetFrom.getLastRow(), 1).getValues();
sheetTo.getRange(NO_OF_THE_ROW_WHERE_DATA_STARTS,sheetTo.getLastColumn()+1,values.length,1).setValues(values);
}
The above script localizes all the data that needs to be copied by using the getRange() method and is copied by using getValues().
NO_OF_THE_ROW_WHERE_DATA_STARTS is the value representing the number of the row where your data starts (in your case the row of No of LP - Secured);
3 is the value representing the C column (where the data you want to be copied is located);
sheetFrom.getLastRow() is the value representing the end of the data you want to be copied;
1 is the value representing the number of columns that need to be copied.
Afterwards, the getRange() method is used again in order to be able to identify where the data needs to be pasted and setValues() in order to actually paste it.
Note: The above script works for different spreadsheets and/or different sheets OR for sheets in the same spreadsheet. If you want to use it for the latter case, you just have to put the name of your sheet instead of SHEET_FROM and SHEET_TO.
Moreover, I suggest you check the following links since they might be of help:
Sheet Class Apps Script - getRange();
Range Class Apps Script - getValues();
Range Class Apps Script - setValues();
Sheet Class Apps Script - getLastColumn();
Sheet Class Apps Script - getLastRow();

Google sheets Script with 2 conditions to move row to new sheet

I am trying to figure a google sheets problem. I have a master sheet (which is a form response sheet) And I need to copy data to indivdual contractors pages (there are 22 or more individual contractors) Line 22 or next available after line 22) IF Move is selected in F column. I could do this with a query Importrange. The problem is I want this to stay on the sheet it is moved to. So not copied actually moved from the master sheet to the contractor sheet. When the contractors name is selected AND Move is selected in the F column. I do not know how to do that. But I am guessing since there are so many contractors I will need something on the contractors sheet or? a single script for each contractor? or I am over thinking this.
Master Sheet:
https://docs.google.com/spreadsheets/d/1PqXc1XDyhg3nGSia4klxsuoKQtmPcBiDBrgJQr5uwyE/edit?usp=sharing
Contractor Page
https://docs.google.com/spreadsheets/d/1k3as6E30cytNfkzHC55clEH5Jik90aIXOrkgsjRXgEs/edit?usp=sharing
I have tried:
=QUERY(IMPORTRANGE("ID HERE","IN Order!A1:B"),"SELECT * WHERE Col1 = 'Grace'")

Google Apps Script- Copy & paste data from column one row at a time

(Please note: JavaScript, Stack Overflow posting, & Google Script novice)
THE BACKGROUND: I'm attempting to use a Google Sheet Email Notification Add On to trigger notifications when a specific value is seen in a Google Sheet. The config requirements of the add on look for a TextValue, however it has trouble finding the value in a pivot table (where my data is sorted). I've tried moving the data to another sheet with a QUERY formula, however the resultant value is not seen by the add on as a TextValue but rather a cell formula.
My workaround to this is to use a script to copy & paste the data from my pivot to a separate sheet. I used this post as my guide to create my script. It works, however because it pastes all column values at once, the add on is having trouble identifying the trigger TextValue.
THE QUESTION: instead of having the entire column copied and pasted at once, how can I tweak this script to copy and paste a row at a time with a slight delay (1-2 seconds)? This delay will allow the add on to see the the target TextValue and execute the notification (I've tested this manually and it works as expected). Any help in modifying this script (or completely changing the logic if necessary) would be greatly appreciated!!
//Copy contents of inventory column daily to run notification add on
function readInventoryNum() {
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Inventory Totals");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Testing123");
// Copy from 2nd row, 7th column, all rows for one column
var valuesToCopy = sheetFrom.getRange(2, 7, sheetFrom.getLastRow(), 1).getValues();
//Paste to another sheet from first cell onwards
sheetTo.getRange(1,sheetTo.getLastColumn()+1,valuesToCopy.length,1).setValues(valuesToCopy);
}

Hard link cells in script editor for Google Spreadsheet

When in Google Spreadsheet, you have formula's referencing cells to make for example a simple sum, Spreadsheets will automatically update the references, should the cells be moved. For example if the cells where cut and pasted to a different location, or rows/columns where added somewhere.
I've got this Spreadsheet where I refresh some data in using REST API's in the Spreadsheet script editor. This one runs every 5 minutes or so. However if the cells in which the data needs to be inserted, move around, my script breaks. So I have a list of constants with all kinds of cell names, for example:
/* --- EXCHANGE RATES --- */
var CELL_BTC_EUR = "B3";
var CELL_ETH_EUR = "B4";
var CELL_BNK_EUR = "B5";
But I need to update these every time I move something to optimize the spreadsheet. Is there a way to hard link it to a specific cell so that they automatically update similar to in cell formula's? Maybe not with these constant but with lines like:
sheet.getRange(CELL_BTC_EUR).setValue(btceur.last);
Potential solution
I make a seperate sheet with all my "dynamic" data which is refreshed via Spreadsheet script. In my main sheet, I can reference those cells. Thus if in my mainsheet things start moving, the reference to my other sheet stay the same. I can then hide/protect the Script Sheet.
I'm still gonna make a small table in my mainsheet with my currency exchange rates, just to get an overview of everything. But my references would not break anymore everytime I insert a new coin or something.
(better solutions are welcome. :) )

Google Spreadsheets with Form Vinculated copy

I'm trying to copy a spreadsheet with the responses of a form thought drive API.
https://developers.google.com/drive/v2/reference/files/copy
All files from my drive are working fine but only this spreadsheet that persist my form answers that create a duplicate form instead of only copying the spreadsheet itself.
You can try to reproduce the problem using the given Id. After copying you will notice that both spread sheet and form will be copied. This should not be a problem if I could erase the form but in the response of the copy procedure I don't get any advice about the form that is being copied together.
File id: 0Aqq-9JjR-lUydHRKVEJ2SThGMjJlVjVqczkyWlVCWUE
Please, help me. I'm desperate.
If you are only trying to copy the spreadsheet of the form, try this:
var fromSheet = ***whatever***;//this is the sheet attached to your form
var toSheet = ***whatever***;//this will be the sheet that you are copying to
var range = **whatever***;//this is the range you are copying over. If you are using dynamic ranges (ie varying number of rows), you may want to **getDataRange()** and **getLastRow** to build a more flexible range
function myCopyCat(){
myValues = fromSheet.getRange(range).getValues();//'copies' all of your data within range from fromSheet to an array
toSheet.getRange(range).setValues(myValues);//'pastes' all of your data into cells on toSheet
}