My script quit working for google sheets. I can't see why line 7 is hanging up.
Here is the script
function onEdit(event)
{
var timezone = "GMT-7";
var timestamp_format = "MM-dd-yyyy HH:mm:ss";
var updateColName = "Narrative";
var timeStampColName = "Time Stamp";
var sheet = event.source.getSheetByName('Narrative');
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(2, 2, 2, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) {
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Any help would be appreciated!
Has the name of the sheet changed?
var sheet = event.source.getSheetByName('Narrative');
Related
Trying to get the formatting copied over from the front sheet, but it takes the forumla which is a import range, is there any way to achieve this without showing the ref error.
function BrandSheets() {
var ss = SpreadsheetApp.getActive();
var Frontsheet = ss.getSheets()[0];
var Header_range = Frontsheet.getRange(1,1,1,13);
var CurrentBrand = Frontsheet.getRange(2,3).getValue();
var CurrentTopBrand = 2;
var CurrentBottomBrand = 2;
var CheckBrand = CurrentBrand;
Frontsheet.getRange(1,3).activate();
Frontsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
var FLastCell = Frontsheet.getActiveCell();
var FLastRow = FLastCell.getRow();
for(var i = 2;i<=FLastRow+1;i++){
var CheckBrand = Frontsheet.getRange(i,3).getValue();
if (CheckBrand == CurrentBrand){
var CurrentBottomBrand = i
}
else{
var CurrentBottomBrand = i
var BrandTable = Frontsheet.getRange(CurrentTopBrand,1,CurrentBottomBrand-CurrentTopBrand,13);
var NewSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(CurrentBrand);
var NewHeader_range = NewSheet.getRange(1,1,1,13);
var NewBrandTable = NewSheet.getRange(2,1,CurrentBottomBrand-CurrentTopBrand,13);
Header_range.copyTo(NewHeader_range,{contentsOnly:true});
BrandTable.copyTo(NewBrandTable);
var CurrentTopBrand = i;
var CurrentBrand = Frontsheet.getRange(i,3).getValue();
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach( sheet => sheet.setTabColor("grey"));
}
}
}
I tried Header_range.copyTo(NewHeader_range,{contentsOnly:true,formatOnly:true});
But this is where the bug of ref comes up. Issue Ref
Many thanks,
I'm using the following formula, and I'm getting: The resulting array was too large.
Any ideas? I have over 20k rows.
UPDATE: If I can accomplish this formula with a script, that would be great. Thanks!
=UNIQUE(ARRAYFORMULA({IMPORTRANGE(Links!B2,"uno!A:AF");IMPORTRANGE(Links!B3,"dos!A:AF");IMPORTRANGE(Links!B4,"tres!A:AF");IMPORTRANGE(Links!B5,"cuatro!A:AF");IMPORTRANGE(Links!B6,"cinco!A:AF")}))
Try by script this way
function test() {
var dest = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('uno')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('dos')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id3')
var sh = ss.getSheetByName('tres')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id4')
var sh = ss.getSheetByName('cuatro')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id5')
var sh = ss.getSheetByName('cinco')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
};
I need to automatically copy a row when a certain value is given.
With Moveto it works, but with copyto it does not.
Can someone help me? tx
function myFunction3() {
var sheetNameToWatch = "Planning";
var columnNumberToWatch = 12;
var valueToWatch = "Running";
var sheetNameCopyTheRowTo = "Facturatie";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch
&& range.getColumn() == columnNumberToWatch
&& range.getValue() == valueToWatch)
{
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(),1,1,sheet.getLastColumn()).copyTo(targetRange);
}
}
I tried to convert a value to another (from angstrom to arshin, a Russian ancient value of length), but often I got result in exponential style 1.837684e-10. I need to show it in decimal style 0.0000000001837.
The code I use:
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = NumberFormatter.Style.decimal
if let text = textField.text, let number = formatter.number(from: text) {
angstrem = number.doubleValue
arshin = Double(angstrem * 7112000000.0000)
arshinLabel.text = "\(arshin)"
}
Adding this line is got nothing:
formatter.numberStyle = NumberFormatter.Style.decimal
UPD
Full code down there:
import UIKit
class AngstremViewController: UIViewController {
var angstrem: Double = 0
var arshin: Double = 0
var versta: Double = 0
var vershok: Double = 0
var decimetr: Double = 0
var duim: Double = 0
var kilometr: Double = 0
var metr: Double = 0
var mili: Double = 0
var millimetr: Double = 0
var mormil: Double = 0
var sajen: Double = 0
var santimetr: Double = 0
var fut: Double = 0
var yard: Double = 0
#IBOutlet weak var textField: UITextField!
//Result labels
#IBOutlet weak var angstremLabel: UILabel!
#IBOutlet weak var arshinLabel: UILabel!
#IBOutlet weak var verstaLabel: UILabel!
#IBOutlet weak var vershokLabel: UILabel!
#IBOutlet weak var decimetrLabel: UILabel!
#IBOutlet weak var duimLabel: UILabel!
#IBOutlet weak var kilometrLabel: UILabel!
#IBOutlet weak var metrLabel: UILabel!
#IBOutlet weak var miliLabel: UILabel!
#IBOutlet weak var millimetrLabel: UILabel!
#IBOutlet weak var mormiliLabel: UILabel!
#IBOutlet weak var sajenLabel: UILabel!
#IBOutlet weak var santimetrLabel: UILabel!
#IBOutlet weak var futLabel: UILabel!
#IBOutlet weak var yardLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let toolBar = UIToolbar()
toolBar.sizeToFit()
toolBar.tintColor = UIColor.init(red:47/255.0, green:158/255.0, blue:249/255.0, alpha: 1.0)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let doneButon = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked))
toolBar.setItems([flexibleSpace, doneButon], animated: true)
textField.inputAccessoryView = toolBar
}
#objc func doneClicked() {
view.endEditing(true)
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = NumberFormatter.Style.decimal
if let text = textField.text, let number = formatter.number(from: text) {
angstrem = number.doubleValue
arshin = Double(angstrem * 0.00000000014060742)
versta = Double(angstrem * 0.00000000000009373828270000001)
vershok = Double(angstrem * 0.0000000022497188)
decimetr = Double(angstrem * 0.000000001)
duim = Double(angstrem * 0.0000000039370078700000005)
kilometr = Double(angstrem * 0.0000000000001)
metr = Double(angstrem * 0.0000000001)
mili = Double(angstrem * 0.000000000000062137119)
millimetr = Double(angstrem * 0.0000001)
mormil = Double(angstrem * 0.00000000000005399568)
sajen = Double(angstrem * 0.000000000046869141)
santimetr = Double(angstrem * 0.00000001)
fut = Double(angstrem * 0.00000000032808399)
yard = Double(angstrem * 0.00000000010936132999999999)
arshinLabel.text = "\(arshin)"
verstaLabel.text = "\(versta)"
vershokLabel.text = "\(vershok)"
decimetrLabel.text = "\(decimetr)"
duimLabel.text = "\(duim)"
kilometrLabel.text = "\(kilometr)"
metrLabel.text = "\(metr)"
miliLabel.text = "\(mili)"
millimetrLabel.text = "\(millimetr)"
mormiliLabel.text = "\(mormil)"
sajenLabel.text = "\(sajen)"
santimetrLabel.text = "\(santimetr)"
futLabel.text = "\(fut)"
yardLabel.text = "\(yard)"
}
}
}
If i convert this
let number = formatter.number(from: text)
to
let number = formatter.String(from: number)
I've got errors.
I understand method but in all cases it doesn't working.
Could someone help with it?
So first of all what you did does not use a formatter at all (as already mentioned by #Martin R). You need to use formatter.string(from: <#number#>). But it will not work this way either, it will most likely display "0". To simulate your result you would need to use .scientific style.
So what you are looking for is .decimal style that includes minimumFractionDigits which will force displaying a number of digits you need. Another probably better way is using minimumSignificantDigits. This will always show at least N digits that are non-zero (kind-of, 1001 still counts as 4 non-zero).
Another issue is that when dealing with such values you should not be using double at all. You need NSDecimalNumber which is designed for precision and large values.
Check this following example that combines it all:
let firstValue = NSDecimalNumber(string: "0.0000000001837")
let secondValue: NSDecimalNumber = 0.00001
let overallValue = firstValue.multiplying(by: secondValue)
let formatter = NumberFormatter()
formatter.locale = .current
formatter.numberStyle = .decimal
formatter.minimumSignificantDigits = 4
if let text = formatter.string(from: overallValue) {
print(text)
}
This will print the result of 0,000000000000001837. If I increase minimumSignificantDigits to 6 then the result will be 0,00000000000000183700.
I've converted you code in playground that you can find helpful:
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = NumberFormatter.Style.decimal
let text = "0.0000000001837"
//! Convert string to number
let number = formatter.number(from: text)!
// 1.837e-10
let stringFromNumber = formatter.string(from: number)
//0
// Set up maximum number of fractional digits in formatter
formatter.maximumFractionDigits = 100
let fullStringFromNumber = formatter.string(from: number)
//"0.0000000001837"
let angstrem = number.doubleValue
let arshin = Double(angstrem * 7112000000.0000)
//1.3064744
let arshinNumber = NSNumber(value: arshin)
let arshinStr = formatter.string(from: arshinNumber)
//"1.3064744"
Thanks to #matic-oblak !
The solution is:
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .decimal
formatter.minimumSignificantDigits = 6
if let text = textField.text, let number = formatter.number(from: text) {
angstrem = number.doubleValue
arshinLabel.text = formatter.string(from: NSDecimalNumber(value: angstrem).multiplying(by: 0.00000000014060742))
}
Can I put this in a loop instead of having to manually code it in for each chapter?
var chp1 = Array(Chapters[0].componentsSeparatedByString("\n"))
var chp2 = Array(Chapters[1].componentsSeparatedByString("\n"))
var chp3 = Array(Chapters[2].componentsSeparatedByString("\n"))
var chp4 = Array(Chapters[3].componentsSeparatedByString("\n"))
var chp5 = Array(Chapters[4].componentsSeparatedByString("\n"))
var chp6 = Array(Chapters[5].componentsSeparatedByString("\n"))
var chp7 = Array(Chapters[6].componentsSeparatedByString("\n"))
var chp8 = Array(Chapters[7].componentsSeparatedByString("\n"))
Yes you can use
var chp = []
for chapter in Chapters
{
chp.append(chapter.componentsSeparatedByString("\n")))
}
now you can access to the chapters like
chp[0]
chp[1]
chp[2]
Also you don't need to cast return value to Array.
for range in 0...8 {
var chp = Chapters[range]
let result = chp.componentsSeparatedByString("\n"))
}
Or
for (_, chapter) in Chapters.enumerate() {
let result = chapter.componentsSeparatedByString("\n"))
}