Google表格的Google脚本-范围复制粘贴特殊的“添加”

时间:2018-11-25 08:43:09

标签: google-apps-script google-sheets

我真的是脚本的新手,我曾经在Microsoft Office上工作,该软件具有特殊的粘贴“添加”功能,现在对于Google工作表我找不到它。

我将在同一张纸D2:D102上具有C2:C102和目标范围的源范围,我希望脚本(我可以每周手动运行)从源复制所有范围并将其与D2处已存在的数据求和:D102(仅值)。

这是一个小例子-Before after

我尝试使用此代码,但是ofc只是替换了这些值。

function copyCells(){
   var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var SourceSheet = thisSpreadsheet.getSheetByName("test");
  var SourceRange = thisSpreadsheet.getRange("C2:C102");

  var destinationSheet = thisSpreadsheet.getSheetByName("test");
  var destinationRange = destinationSheet.getRange("D2:D102");

   SourceRange.copyTo(destinationRange, {contentsOnly: true}); 
}

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

还没有测试代码,但是可以尝试一下。

  • 使用 class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { //MARK: Outlets @IBOutlet weak var uploadButtonOutlet: UIBarButtonItem! @IBOutlet weak var collectionView: UICollectionView! //MARK:- Properties var images: [UIImage] = [] var factsArray:[Facts] = [Facts]() var likeUsers:[String] = [] let currentUser = Auth.auth().currentUser?.uid private let refreshControl = UIRefreshControl() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. if #available(iOS 10.0, *) { collectionView.refreshControl = refreshControl } else { collectionView.addSubview(refreshControl) } refreshControl.addTarget(self, action: #selector(refreshView), for: .valueChanged) refreshControl.tintColor = UIColor.white if let layout = collectionView?.collectionViewLayout as? FactsFeverLayout { layout.delegate = self } collectionView.backgroundColor = UIColor.black observeFactsFromFirebase() } @objc func refreshView(){ observeFactsFromFirebase() } //MARK:- Upload Facts @IBAction func uploadButtonPressed(_ sender: Any) { self.selectPhoto() (deleted the function of selectPhoto but it works, UIImagePicker is used) } private func uploadImageToFirebaseStorage(image: UIImage, completion: @escaping (_ imageUrl: String) -> ()){ let imageName = NSUUID().uuidString + ".jpg" let ref = Storage.storage().reference().child("message_images").child(imageName) if let uploadData = image.jpegData(compressionQuality: 0.2){ ref.putData(uploadData, metadata: nil, completion: { (metadata, error) in if error != nil { print(" Failed to upload Image", error) } ref.downloadURL(completion: { (url, err) in if let err = err { print("Unable to upload image into storage due to \(err)") } let messageImageURL = url?.absoluteString completion(messageImageURL!) }) }) } } func addToDatabase(imageUrl:String, caption: String, image: UIImage){ let Id = NSUUID().uuidString likeUsers.append(currentUser!) let timeStamp = NSNumber(value: Int(NSDate().timeIntervalSince1970)) let factsDB = Database.database().reference().child("Facts") let factsDictionary = ["factsLink": imageUrl, "likes": likeUsers, "factsId": Id, "timeStamp": timeStamp, "captionText": caption, "imageWidth": image.size.width, "imageHeight": image.size.height] as [String : Any] factsDB.child(Id).setValue(factsDictionary){ (error, reference) in if error != nil { print(error) ProgressHUD.showError("Image Upload Failed") self.uploadButtonOutlet.isEnabled = true return } else{ print("Message Saved In DB") ProgressHUD.showSuccess("image Uploded Successfully") self.uploadButtonOutlet.isEnabled = true self.observeFactsFromFirebase() } } } var imageUrl: [String] = [] func observeFactsFromFirebase(){ let factsDB = Database.database().reference().child("Facts").queryOrdered(byChild: "timeStamp") factsDB.observe(.value){ (snapshot) in print("Observer Data snapshot \(snapshot.value)") self.factsArray = [] self.imageUrl = [] self.likeUsers = [] if let snapshot = snapshot.children.allObjects as? [DataSnapshot] { for snap in snapshot { if let postDictionary = snap.value as? Dictionary<String, AnyObject> { let id = snap.key let facts = Facts(dictionary: postDictionary) self.factsArray.insert(facts, at: 0) self.imageUrl.insert(facts.factsLink, at: 0) } } } self.collectionView.reloadData() self.refreshControl.endRefreshing() } collectionView.reloadData() } // Download Image From Database //-> Here I download image from firebase and store it locally and append it to images array (Deleted the code to remove unwanted clutter) } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// //MARK: Data Source extension ViewController: UICollectionViewDataSource{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return factsArray.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let facts = factsArray[indexPath.row] let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "newCellTrial", for: indexPath) as? NewCellCollectionViewCell cell?.configureCell(fact: facts) cell?.infoButton.addTarget(self, action: #selector(reportButtonPressed), for: .touchUpInside) return cell! } } extension ViewController: UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { collectionView.deselectItem(at: indexPath, animated: true) let photos = IDMPhoto.photos(withURLs: imageUrl) let browser = IDMPhotoBrowser(photos: photos) browser?.setInitialPageIndex(UInt(indexPath.row)) self.present(browser!, animated: true, completion: nil) } } extension ViewController: FactsFeverLayoutDelegate { func collectionView(CollectionView: UICollectionView, heightForThePhotoAt indexPath: IndexPath, with width: CGFloat) -> CGFloat { let facts = factsArray[indexPath.item] let imageSize = CGSize(width: CGFloat(facts.imageWidht), height: CGFloat(facts.imageHeight)) let boundingRect = CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT)) let rect = AVMakeRect(aspectRatio: imageSize, insideRect: boundingRect) return rect.size.height } func collectionView(CollectionView: UICollectionView, heightForCaptionAt indexPath: IndexPath, with width: CGFloat) -> CGFloat { let fact = factsArray[indexPath.item] let topPadding = CGFloat(8) let bottomPadding = CGFloat(8) let captionFont = UIFont.systemFont(ofSize: 15) let viewHeight = CGFloat(40) //-> There is view below caption which holds like button and info button its height is constant (40) let captionHeight = self.height(for: fact.captionText, with: captionFont, width: width) let height = topPadding + captionHeight + topPadding + viewHeight + bottomPadding + topPadding + 10 return height } func height(for text: String, with font: UIFont, width: CGFloat) -> CGFloat { let nsstring = NSString(string: text) let maxHeight = CGFloat(1000) let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin) let textAttributes = [NSAttributedString.Key.font: font] let boundingRect = nsstring.boundingRect(with: CGSize(width: width, height: maxHeight), options: options, attributes: textAttributes, context: nil) return ceil(boundingRect.height) } } 抢夺值
  • 将值求和
  • 使用getValues()

    复制值
    setValues()

参考

range.getValues()

range.setValues()